-2

so i have a file called test.txt that contain the following lines.

  1. The first school , located at random at 0000
  2. The second school , located at random at 1000
  3. The third school , located at random at 2000
  4. The fourth school , located at random at 3000

I want use sed to list the only school number and school name so the output is

  1. The first school
  2. The second school
  3. The third school
  4. The fourth school

The way im doing this by using the following command. sed 's/[,].*//' test.txt

This replaces the comma and everything after with blank spaces. Is there another way i can achieve this using only sed?

  • What’s wrong with that way? – Jeff Schaller Feb 14 '18 at 0:54
  • nothings wrong, but im curious if there is another method. I'll have sort this by name later so I was wondering if there is way to extract this part instead of replacing rest of the line with blank spaces – Rashad Feb 14 '18 at 0:57
  • Just to clarify, your sed command doesn’t introduce blank spaces. – Jeff Schaller Feb 14 '18 at 1:00
  • what is the rest of the line being replaced with – Rashad Feb 14 '18 at 1:01
  • I'm really not understanding what you're asking here. You got it pretty much with sed on how to remove everything after the ,. Another way you could do it that is not sed is with awk. awk -F, '{print $1}' test.txt – Terrance Feb 14 '18 at 1:03
0

No need a character class [ ]:

sed 's/,.*//' test.txt
  • Thanks, but is there another method for this. ? – Rashad Feb 14 '18 at 0:43
  • This is the simplest one, dunno what you expect :D – Gilles Quenot Feb 14 '18 at 1:09

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.