Home > Back-end >  How to use `cut` or `sed` to delete few characters or a word only from the beginning of output and n
How to use `cut` or `sed` to delete few characters or a word only from the beginning of output and n

Time:05-28

Generically speaking, Given a possible some command output like this:

one two three four five
six seven eight
one nine five two
etc...

I want to use sed or cut (or possibly other command that doesn't require additional installation on macOS) to remove just the first one at the beginning of the stream. Just the first word on the first line. All the rest of the output should be unaffected (i.e. one on the third line should remain in the output). How can I do that? (All SO posts I've seen do it for every line, which is not what I want.)

Additional context: I need to run a remote command via ssh that requires sudo. My script collects the password from users and then echos it into ssh's stdin, and the command ssh runs is sudo -S. -S causes sudo to read password from stdin, but it still echos "Password:" to stderr. In order not to confuse user with second pointless prompt, I want to zap it out from stderr output, but I do want the rest of the stderr output in case command that sudo runs does generate stderr output.

CodePudding user response:

You can write a sed script that only applies to the first line. For example,

$ sed '1s/one //' tmp.txt
two three four five
six seven eight
one nine five two
etc...

The address 1 means that the s command is not applied to any line except line 1.

CodePudding user response:

Using sed

$ sed 's/one \(.*\)/\1/;:a;n;ba' input_file
two three four five
six seven eight
one nine five two
etc...

CodePudding user response:

Using awk and removing any first word (separated by any number of spaces):

awk 'NR==1 {gsub($1 " *", "")} {print}'
  • Related