Computer And Technologies

Computer And Technologies: Unix Interview questions (sed)

Thursday, 13 August 2009

Unix Interview questions (sed)

> remove first column of a space delimited txt
how to remove the first column of a space delimited txt file? there are 12+ columns... what is the cleanest way?

cut -d\ -f4-
OR
cat | cut -d\ -f4-
OR
sed -e 's/^[ \t]*//' filename

>delete 7th column in unix,

cut -f1-6,8- filename

>how to remove spaces in a string using sed.

`echo "$infilename" | sed 's/^ *//;s/ *$//`

>replace space or spaces in a line of a file with a single :

sed -e 's/\s/:/g'

>sed : remove whitespace

sed 's/ $//' file1.txt > file2.txt # Remove the last space at eol
sed 's/ *$//' file1.txt > file2.txt # Remove all spaces at eol

>To remove all whitespace (including tabs) from left to first word, enter:

cat | sed -e 's/^[ \t]*//'
Where,

* s/ : Substitute command ~ replacement for pattern (^[ \t]*) on each addressed line
* ^[ \t]* : Search pattern ( ^ - start of the line; [ \t]* match one or more blank spaces including tab)
* // : Replace (delete) all matched pattern

No comments:

Post a Comment