开发者

Explained shell statement

The following statement will remove line numbers in a txt file:

cat withLineNumbers.txt | sed 's/^.......//' >> withoutLineNumbers.txt

The input file is cr开发者_开发百科eated with the following statement (this one i understand):

nl -ba input.txt >> withLineNumbers.txt

I know the functionality of cat and i know the output is written to the 'withoutLineNumbers.txt' file. But the part of '| sed 's/^.......//'' is not really clear to me.

Thanks for your time.


That sed regular expression simply removes the first 7 characters from each line. The regular expression ^....... says "Any 7 characters at the beginning of the line." The sed argument s/^.......// substitutes the above regular expression with an empty string.

Refer to the sed(1) man page for more information.


that sed statement says the delete the first 7 characters. a dot "." means any character. There is an even easier way to do this

 awk  '{print $2}' withLineNumbers.txt

you just have to print out the 2nd column using awk. No need to use regex

if your data has spaces,

awk  '{$1="";print substr($0,2)}' withLineNumbers.txt


sed is doing a search and replace. The 's' means search, the next character ('/') is the seperator, the search expression is '^.......', and the replace expression is an empty string (i.e. everything between the last two slashes).

The search is a regular expression. The '^' means match start of line. Each '.' means match any character. So the search expression matches the first 7 characters of each line. This is then replaced with an empty string. So what sed is doing is removing the first 7 characters of each line.

A more simple way to achieve the same think could be:

cut -b8- withLineNumbers.txt > withoutLineNumbers.txt
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜