bash command to remove lines which are present in other text file [duplicate] - linux

This question already has answers here:
Deleting lines from one file which are in another file
(11 answers)
Closed 3 years ago.
I am on bash I have two files added.txt and unmatched.txt , now imagine that all lines from added.txt are present in unmatched.txt . I want to remove lines from unmatched.txt which are present in added.txt . for example
1) added.txt
apple
ball
2) unmatched.txt
cat
dog
apple
rar
ball
3) required output.txt
cat
dog
rar

Trivial to do with grep:
$ cat added.txt
cat
dog
$ cat unmatched.txt
aardvark
cat
dog
giraffe
civet cat
$ grep -F -vx -f added.txt unmatched.txt
aardvark
giraffe
civet cat
Prints just lines of unmatched.txt that don't exactly match lines of added.txt (-v inverts the usual meaning of grep).

Related

How to Save 'specific' line from terminal output to file? [duplicate]

This question already has answers here:
Bash tool to get nth line from a file
(22 answers)
Closed 4 years ago.
I am currently using the following to save terminal outputs to file:
$command -someoptions >> output.txt
However, I am only interested in one line from the terminal output.
Is there a way to do this by changing the above expression. Or will I have to delete lines after the 'output.txt' file is formed?
For example: If my output is:
line 1
line 2
line 3
line 4
line 5
and all I want to save is:
line 4
where line 4 contains unknown information.
I am asking as I will later wish to script this command.
Many thanks,
Solution Found:
I ended up using:
$command -someoptions | sed -n '4p' >> output.txt
This is a classic simple grep issue.
$command -someoptions | grep 'line 4' >> output.txt
You could refine that with more pattern complexity, and might need it depending on how precisely you need to match the data.
Try with this command:
$command -someoptions | grep " filter " >> output.txt
filter must be replaced by an element that distinguishes your line 4 from the other lines.

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

How can I get a whole word with grep? [duplicate]

This question already has answers here:
Display exact matches only with grep [duplicate]
(9 answers)
How to make grep only match if the entire line matches?
(12 answers)
Closed 5 years ago.
I want to get the lines that contain a defined word with grep.
Edit: The solution was this one.
I know that I can use the -w option, but it doesn't seems to do the trick.
For example: every word that contains my defined word separated by punctuation signs is included. If I look for dogs, it will show me lines that contain not only dogs word but also cats.dogs, cats-dogs, etc.
# cat file.txt
some alphadogs dance
some cats-dogs play
none dogs dance
few dog sing
all cats.dogs shout
And with grep:
# cat file.txt | grep -w "dogs"
some cats-dogs play
none dogs dance
all cats.dogs shout
Desired output:
# cat file.txt | grep -w "dogs"
none dogs dance
Do you know any workaround that allows you to get the whole word? I've tested it with \b or \< with negative results.
Thanks,
Eudald
Use the word-boundary anchors, in any version of grep you have installed
grep '^dogs$' file.txt
An excerpt from this regular-expressions page,
Anchors
[..] Anchors do not match any characters. They match a position. ^ matches at the start of the string, and $ matches at the end of the string.[..]
Try with -x parameter:
grep -x dogs file.txt
From grep manual:
-x, --line-regexp
Select only those matches that exactly match the whole line. (-x is specified by POSIX.)
NOTE: cat is useless when you pipe its output to grep

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

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

How to copy or delete specific lines in bash and create them into a new file [duplicate]

This question already has answers here:
How to delete from a text file, all lines that contain a specific string?
(21 answers)
Closed 7 years ago.
I have an auto_generated file that could have duplicate data, which causes my parser to crash. How can I check line-by-line and remove the unwanted lines based on a character that it has on bash ? For example:
for line in file.txt:
if '(1)' in line:
delete line
elif '(2)' in line:
delete line
elif '(3)' in line:
delete line
else:
return (file.txt with those lines removed )
Sample Input
Hello my name is john
Hello my name is eric
Hello my name is jonh(2)
Hello my name is ray
Hello my name is john (1)
Hello my name is eric (3)
Sample Output
Hello my name is john
Hello my name is eric
Hello my name is ray
To exclude lines that have pattern ( + letter + ), you could do:
grep -v '(.)' file
If you want the letter to be a number:
grep -v '([0-9])' file
If you want to exclude a single specific number:
grep -v '(1)' file
If you want to exclude multiple specific numbers:
grep -v '([123])' file
If you want to exclude multiple different patterns:
grep -v -e pattern1 -e pattern2 -e pattern3 file

Resources