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:


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!


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.