insert underscore in columns of a text file shell [duplicate] - linux

This question already has answers here:
Replace whitespace with a comma in a text file in Linux
(9 answers)
Closed 6 years ago.
I have a tab separated text file f.txt like :
APPLE 10 5
BALL 20 6
CAT 30 7
I want the output to be
APPLE_10_5
BALL_20_6
CAT_30_7
I wrote the following to partially accomplish this, but I am stuck at the "paste" step. Can you help?
cat f.txt | cut -f 1,2,3 | paste ???

When the are sperated by one space using sed is a one liner.
sed -i "s/ /_/" input.txt

Related

How to grep a word from a specific line till the end of file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How to find a word for eg
Find "abc" starting from line 6 till the end of file ?
You can grep the output of tail command like this.
tail -n+6 file.txt | grep abc
In sed, it's easy to say "ignore lines in this address range" and then grep in the rest.
sed -n -e '1,5d' -e '/abc/p' file
The -n option says to not print all lines by default, we then simply delete lines 1 through 5, and then print any matching lines in the remainder.
There's also a block syntax, so you can say
sed -n '6,${/abc/p;}' file
but the precise syntax differs slightly between dialects (I think Linux would not demand a semicolon before the closing brace?) The address range 6,$ selects lines from the sixth through the end of the file.
One solution is to use awk, checking both the text and the line number (this script contains only the rule to select lines, it uses the default action which is to print the line):
awk 'NR >= 6 && /abc/' inputfile.txt
The following transcript shows this in action:
pax:~> cat inputfile.txt
1 abc
2 def
3 abc
4 xxx
5 yyy
6 abc
7 xyz
8 abc
9 abc
pax:~> awk 'NR >= 6 && /abc/' inputfile.txt
6 abc
8 abc
9 abc
Delete (d) lines from 1 to 5 (1,5); and also delete (the other d) lines not (!) containing abc (/abc/):
sed '1,5d;/abc/!d' input.txt

usding sed to replace multi-words [duplicate]

This question already has an answer here:
replace multiple strings in one line with sed
(1 answer)
Closed 4 years ago.
Is there an example of if you want to replace multi-words with one word
for example input: dog apple orange banana pear
output dog cat cat banana cat
I have solved with this below is there is a better way(better than sed)? by not typing cat three times:
sed -e 's/apple/cat/g;s/orange/cat/g;s/pear/cat/g'
thanks in advance
Simply replace the strings at once using OR operator
sed -e 's/apple\|orange\|pear/cat/g' filename

Remove the first 6 columns using Linux [duplicate]

This question already has answers here:
how to remove the first two columns in a file using shell (awk, sed, whatever)
(9 answers)
Closed 4 years ago.
From a text file, I want to remove the first 6 columns. I tried sed as follows, but I have to do it six times (one for each column). Is there any efficient way to do it (or pass the 6 columns at once for sed)?
sed -i -r 's/(\s+)?\S+//1' file
Thanks!
You could do this within regex and quantifying braces if a column consists of non-whitespace characters with optional leading spaces:
sed -i -r 's/^((\s+)?\S+){6} *//' file

How to tail all lines except first row [duplicate]

This question already has answers here:
how to read file from line x to the end of a file in bash
(7 answers)
Closed 7 years ago.
For example, I have a file
1
2
3
then I want to output from 2nd row to tail
How can I do it in linux
tail -n+2 my_file
will output all the lines in myfile starting with line 2. (-n2 would show you the last two lines.)
tail has lots more options. Type man tail for complete documentation.
shorter with
$ sed 1d filename
or with awk
$ awk 'NR>1' filename

How can I use awk to display some certain fields in a text file? [duplicate]

This question already has answers here:
How to print third column to last column?
(19 answers)
Closed 9 years ago.
I have a text file like this:
1 http 2 3 4 5
2 dns 3 4
3 ftp 3 4 5 6 8
I want the output to be like this:
http 2 3 4 5
dns 3 4
ftp 3 4 5 6 8
Node that I just want to omit the first column in that file and the fields number in a certain line is not fixed.
Can I accomplish this goal using awk?
You can also use cut: cut -d' ' -f2-.
Edit: If you have to use awk, try awk '{$1=""; print $0}'
something like this maybe?
awk '{$1 =""; print }' file
If you don't care about the the field separators remaining in place, you can do this:
awk '{$1=""}1' filename
(Assuming filename is where you stored your data)
Drats, I was going to give you an Awk solution, then recommend cut. It looks like others have beaten me to the punch.
However, I see no sed solution yet!
$ sed -n `s/^[^ ][^ ]*//p` yourfile.txt
sed 's/^..//g' your_file
above should work based on the condition that the first field is always of a single character.
or in perl:
perl -pe 's/^[^\s]*\s//g' your_file

Resources