0

Currently I am self learning awk and sed. I found the online book Sed & Awk 101 hacks which I found very good so far. Nevertheless, I came across I haven't continue for a few days because I can't get the concept "grouping" Here is the input file

cat employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

The book sed command is the following:

sed 's/\([^,]*\).*/\1/g' employee.txt

The output of this command is:

101
102
103
104
105

I have tried to understand that command but it doesn't make sense to me. Then, after checking the sed documentation (man sed) I noticed that when the flag -E is not used then the regular expression takes the BRE syntax. Could you tell me why this expression doesn't work to get the same input?

sed -E 's/^(\d+),.+/\1/g' employee.txt

Thanks in advance!

1

1 Answer 1

2

The \d doesn't work in sed. You can use [[:digit:]] or [0-9] instead:

sed -E 's/^([[:digit:]]+),.+/\1/' employee.txt
sed -E 's/^([0-9]+),.+/\1/' employee.txt

Note that I removed the g at the end which is not needed in your examples.

Related:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .