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

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.

Related

How can I use sed or grep to delete the first line of input if it contains a string? [duplicate]

This question already has answers here:
How do I match multiple addresses in sed?
(4 answers)
Closed 3 years ago.
I am redirecting input and have 2 fields that I extracted from the tail of an XML file, and I need to ignore the first line if it isn't the first of the 2 entries.
tail -n 327 ~/.local/share/recently-used.xbel | grep -e "<bookmark href=" -e "<mime:mime-type type="
Here is the output from that code, which is working fine, but the problem is that the first line is a
<mime:mime-type type="application/x-shellscript"/>
<bookmark href="file:///usr/local/bin/menu_manager.sh" added="2019-09-17T08:33:48Z" modified="2019-09-17T08:33:48Z" visited="2019-09-17T08:33:49Z">
<mime:mime-type type="application/x-shellscript"/>
I need to look at the first line, and if it contains the string
<mime:mime-type type=
then I need to remove that line and pass the rest of the lines on for the next processing step
I tried
sed '1/<mime:mime-type/d'
But is gives me an error:
sed: -e expression #1, char 2: unknown command: `/'
Try
sed '1{/<mime:mime-type/d}'
which uses a block {} which is only run on line 1, with the delete command in the block.
If you are OK with awk you can use this
awk 'NR!=1 || !/<mime:mime-type type=/'
This prints every line that is not the first line (NR!=1) or doesn't match the pattern (!/<mime:mime-type type=/). As there is no action specified, awk uses the default action print.

How to display exactly 10 lines of a command output [duplicate]

This question already has answers here:
How can I view only the first n lines of the file?
(2 answers)
Closed 4 years ago.
I am working on ARM-based processor and I am preparing inside it a shell script that writes into a text file a set of commands output.
I want it to write exactly 10 lines of a command's output (for example top command) but I don't know how, would you help me please ?
Thank you.
Which operating system are you working in ? If you have awk installed, you
can do:
command | awk 'NR<=10' > f.txt
command | head -n 10 > file.txt
If you want a pure Bash solution:
n=0
command | while (( n++ != 10 )) && IFS= read -r line; do
printf '%s\n' "$line"
done
command | sed 1,10p > f.txt
sed filters lines based on a pattern and performs an action on them. In this case, pattern is to filter lines whose number is between 1 and 10, and action is just to 'p'rint them.

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.

Read a file line by line from last [duplicate]

This question already has answers here:
How can I reverse the order of lines in a file?
(24 answers)
Closed 6 years ago.
I have a requirement to read the file line by line from last line until the first line. Right now I am able to read the file line by line from start with the below piece of code.
while IFS= read line
do
#Logic here
done <"$Input_File"
Kindly help me out with a solution to read the file line by line from last line.
You can use tac to read the file from the last line until the first. Using your example you could do:
while IFS= read line
do
#Logic here
done <<<(tac "$Input_File")
See the manual page for tac (this may not be installed by default in your distribution but should be available using the package manager).
file="path/to/your/file.txt"
awk '{print NR ":" $0}' $file | sort -t: -k 1nr,1 | sed 's/^[0-9][0-9]*://'

add line to a file ONLY if it is not in file already [duplicate]

This question already has answers here:
Appending a line to a file only if it does not already exist
(25 answers)
Closed 1 year ago.
I want to add the following line:
nohup java -jar /mnt/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &
to the end of the file /etc/rc.d/rc.local if it does not already exist.
How can I do that from linux command line? I assume grep or sed would work, but I am not familiar enough with either to get it to work. Right now I use echo, but that just keeps adding it over and over again.
Assuming you want it at the end of the file:
LINE="nohup java -jar /mnt/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &"
FILE=/etc/rc.d/rc.local
grep -q "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
one option is two steps:
grep -q "yourline" /path/file||sed -i '/..place../ a \the line' file
also possible to do with awk,
save all lines in array, during saving if the line was found, exit. otherwise, add the line in END{} block to the right place.
P.S. You didn't tell in the file, where to add that line.

Resources