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

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

Related

grep weird behaviour in linux command line [duplicate]

This question already has answers here:
Using the star sign in grep
(12 answers)
Closed 1 year ago.
I have a file test.txt which contains this text data
# cat test.txt
*#=>*#
if I use grep to check if the string is in the file using this way
# grep "*#=>*#" test.txt
#
it returns nothing..
while if I grep a partial string search
# grep "*#=>" test.txt
# *#=>*#
it works correctly ..
Why in the first case do grep return nothing ?
The asterisk is special to grep, it means "the previous token is repeated zero or more times". So >* would match an empty string or > or >> etc.
To match an asterisk, backslash it in the pattern:
grep '\*#=>\*#' test.txt
(The first asterisk follows no token, so the backslash is optional.)

"No such file or directory" using cut in a while read loop in bash [duplicate]

This question already has answers here:
How to cut an existing variable and assign to a new variable in bash
(1 answer)
printing first word in every line of a txt file unix bash
(5 answers)
Take nth column in a text file
(6 answers)
Bash - While read line from file print first and second column [closed]
(3 answers)
Closed 3 years ago.
I have this sample text file text.txt that is in the form
fruits vegetables
apples cucumbers
oranges squash
and it is tab delimited.
I would like to loop through the file line by line, and extract each column value.
Below is the code the code I have tried.
while read p
do
echo "Line"
fruit="$(cut -f 1 $p)"
echo "${fruit}"
done <test.txt
My expected output should be something like:
Line
fruits
Line
apples
Line
oranges
Instead I get this output:
Line
cut: fruits: No such file or directory
cut: vegetables: No such file or directory
Line
cut: apples: No such file or directory
cut: cucumbers: No such file or directory
Line
cut: oranges: No such file or directory
cut: squash: No such file or directory
I would like to loop through the file line by line, and extract each
column value
Is awk not suitable ?
awk '{ print "Line"; print $1 }' < test.txt

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.

How to delete 1 or more matching line(s) while reading a file in bash script [duplicate]

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Combining two sed commands
(2 answers)
Linux, find replace on a folder of files using a list of items for replacement?
(1 answer)
Closed 4 years ago.
I want to read file using a bash script and delete line(s) which are matching with my specific scenario (line(s) starting with 'z').
my code works fine if the 'inputFile' contains only alphabetic characters.
but, if a line with 'specific characters of sed' (line eg : z-2.10.3.2 x/y/z F (&)[]+* ) then i got an error,(error : sed: -e expression #1, char 29: unterminated `y' command).
#!/bin/bash
inputFile="test.txt"
while IFS= read -r line
do
echo "$line"
if [[ $line == z* ]];
then
sed -i "/$line/d" $inputFile
fi
done < "$inputFile"
i want to delete 'z-2.10.3.2 x/y/z F (&)[]+*' kind of lines, how can i do this...?
As you mentioned you don't need line which has z*
Simply use grep -v
grep -vE "^[[:blank:]]*z" file
I have created one scenario where I have a file which contains
root#ubuntu:~/T/e/s/t# cat file
hello world
sample line 1
sample line 2 world
sample line 3
sample line 4
In my case, I want to remove the line contains "world"
root#ubuntu:~/T/e/s/t# grep -v "world" file
sample line 1
sample line 3
sample line 4
If you want you can redirect your output in another file.

finding specific pattern in linux [duplicate]

This question already has answers here:
Print only matching word, not entire line through grep
(2 answers)
Closed 5 years ago.
I want to find specific pattern in all the files in a directory and copy them to another line
For E.g
I want to find LOG_WARNING in one file XYZ and copy them to another file.
LOG_WARNING (abc, xyz,("WARNING: Error in sending concurrent_ to pdm\n"));
command i have used is :
grep -rin "LOG_WARNING.*" file_name.c > output.txt
but it is not copying till the semicolon, please note that other texts are available in next line. I want to copy till ;(semi-colon)
grep -rh "LOG_WARNING" * > out.txt
This will match the pattern in all the files inside the directory.
Since you mentioned that the texts that are present after the ';' are on the next line, I have provided this command.
This will match the pattern and print the entire line, till the ';'.
Else,
try this
grep -roPh 'LOG_WARNING[^;]*;' * > out.txt

Resources