Search code examples
bashshellsedgnu

printing only specific lines with sed


I have following File (wishlist.txt):

Alligatoah Musik_ist_keine_lösung;https:///uhfhf
Alligatoah STRW;https:///uhfhf?i
Amewu Entwicklungshilfe;https:///uhfhf?i

and want to have the first word of line n. so for n = 1:

Alligatoah

What i have so far is:

sed -e 's/\s.*//g' wishlist.txt

is there a elegant way to get rid of all lines except n?

Edit:

How to pass a bash variable "$i" to sed since

sed -n '$is/ .*//p' $wishlist

and

sed -n "\`${i}\`s/ .*//p" $wishlist

doesn't work


Solution

  • Something like this. For the 1st word of the 3rd line.

    sed -n '3s/\s.*//p' wishlist.txt
    

    To use a variable: Note: Double quotes.

    line=3; sed -n "${line}s/\s.*//p" wishlist.txt