Bash shell script: appending text to a specific line of an existing file without line break [duplicate] - linux

This question already has answers here:
How to add to the end of lines containing a pattern with sed or awk?
(6 answers)
Closed 4 years ago.
I have a file called tmp.mount with the following contents
[Mount]
Options=mode=1777,strictatime,noexec,nosuid
I'd like to search the file via the word Options= to get the
line number.
I will search the word nodev via the given line number.
If it does not exist, I will insert the word ,nodev to the end of
this line via given line number.
With the results
[Mount]
Options=mode=1777,strictatime,noexec,nodev,nosuid,nodev
All without line break. Most of the solution was to use sed but i'm clueless on how I could incorporate the line search with sed.

awk '/Options=/ && ! /nodev/ {print $0 ",nodev"; next};1' file
no need to get the line number, just append the ",nodev" to the corresponding line

Related

How to change file parmater by input in bash [duplicate]

This question already has answers here:
How do I use sed to change my configuration files, with flexible keys and values?
(8 answers)
Closed 2 years ago.
I have a file that contain this line
SELINUX = enforce
I want to change this by given input to permissive
How i do that without demage?
If [[ "$1" == "Y"]]
then
sed -ri 's/(^.*SELINUX=)(.*$)/\1enforce/' file
else
sed -ri 's/(^.*SELINUX=)(.*$)/\1permissive/' file
fi
If the first passed parameter ($1) is equal to "Y" use sed to split SELINUX line into to 2 sections. Substitute the line for the first section followed by "enforce". If the passed parameter is not "Y" substitute the line for the first section followed by "permissive".

"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

Excluding/ignoring (Not Deleting) first and last line based on line number [duplicate]

This question already has answers here:
Delete first and last line or record from file using sed
(5 answers)
Closed 4 years ago.
I'm using sed command to edit some text file.
How can i exclude the first and last line in each text file for being edited.
I went through sed gnu manual but i only found commands to match line ranges i.e. 1,$ or to exclude ranges i.e. 1,$!. i just need to exclude line # 1 and last line $. i'm not sure if its possible to select a range i.e 2, $-1?
Here's my code.
sed -e '1,$ s/.*/<p>&<\/p>/'' file.txt
You can't use maths in sed addresses. But you can tell sed to do nothing on the first and last line:
sed -e '1n; $n; s/.*/<p>&<\/p>/'
where n means "read the next line of input into the pattern space" (in case of the last line, it won't read anything).
(The two single quotes at the end of the expression are probably a typo, right?)

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]*://'

How to add a character to the end of each line in a file in shell script [duplicate]

This question already has answers here:
sed edit file in place
(15 answers)
Closed 8 years ago.
I need a script that add a particular character to the end of each line . I am using the command
sed 's/$/ foo/' r.txt
It adds foo to end of each line in the file r.txt and displays in my terminal .
What do i need to do if i want to save this existing file with this new record appended after the end of each line .
To save to a new file:
sed 's/$/ foo/' r.txt > newfile.txt
To edit in place
sed -i 's/$/ foo/' r.txt

Resources