This question already has answers here:
How to append a string at end of a specific line in a file in bash [duplicate]
(4 answers)
Closed 6 years ago.
I am trying to achieve the following with bash:
Create a file called "file" which contains the following data:
line1
line2
line3456
I want to append additional characters after "line2", for example:
line1
line2 test123
line3456
I know that I can use sed to replace the whole line (sed -i 's/line2/line2 test123/' file). But I am sure that there is a better way to do it.
I intend to modify lines with more than one word in a line so maybe I will need wildcards as well.
sed 's/line2/& test123/' file
If you want to edit your file "in place" use sed's option -i.
Use & as back-reference to the matched pattern:
sed -i 's/word1 word2 word3/& test123/' file
This wil append text " test123" in a line that has word1 word2 word3 text in it.
Related
This question already has answers here:
In-place edits with sed on OS X
(8 answers)
Closed 2 years ago.
For example, I want do remove all lines in a textile that do not contain the character '#'
I have already tried to use sed like so
sed '/#/!d' data.txt
What am I missing? Shouldn't this work?
I prefer using ed over the non-standard sed -i, especially if it needs to be portable:
printf "%s\n" "v/#/d" w | ed -s filename
This deletes every line that doesn't contain a #, and saves the changed file back to disc.
sed -n '/#/p' [file]
-n suppress default printing
/#/ match on # anywhere on the line
p print if it matches
Add -i for in-place editing of the file (if supplied).
This question already has answers here:
How to invert a grep expression
(5 answers)
Closed 3 years ago.
So I've got a CSV file, test.csv, with the following:
Apples
Pears
Oranges
They would be comma separated given its a csv file.
I have a variables
test="app"
I want to remove anything from the csv file with that variable contents in it. So the output would be in a new file test1.csv:
Pears
Oranges
You can use sed to do that.
If you want to remove the entire row that contains test variable's value. Then try-
user#localhost$ sed "/$test/d" test.csv > output.csv
If you want to replace the word that holds the var test, then try -
user#localhost$ replacewith=''
user#localhost$ sed "s/$test/$replacewith/g" test.csv > output.csv
NB: Replace and Delete can mean same thing if you use blank string as replacement.
You can obtain such a result with sed:
sed '/dog/d' inputfile.csv > outputfile.csv
You can also make it case insensitive:
sed '/[dD][oO][gG]/d' inputfile.csv > outputfile.csv
The syntax used in the latter is RegEx, read more about it here.
This question already has answers here:
Bash tool to get nth line from a file
(22 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
So, I am creating a script in bash and I am trying to read a text file which is saved in the same directory. I need to read the nth line of that file and then save it to a variable to be used later but I'm not sure how I can do this
What I have currently tried is listed below but it essentially reads the line from the file, saves it a variable and then deletes that line from the file and repeats. This is a hack and although it works, isn't what I want, I can't get the nth value and it's deleting from the file which I definitely don't want.
read -r first<"test.txt" // reads first line and stores in first
sed -i -e "1d" "test.txt" . // removes first line
read -r second<"test.txt" // reads first line and stores in second
sed -i -e "1d" "test.txt" . // removes first line
If I wanted to get the 2nd line for example, I have seen sed '2q;d' file but not sure how/where the result is saved. It gets printed in terminal? Any help appreciated, thanks!w
sed '2q;d' file
prints the second line in file to the terminal.
To populate a variable with it, use bash's command expansion feature:
$ var=$(sed '2q;d' file)
$ echo "$var"
this is second line
Simple solution using head and tail:
a=$(head -2 test.txt | tail -1 )
Saves the second line of test.txt to the variable $a.
This question already has answers here:
Add a prefix string to beginning of each line
(18 answers)
Closed 7 years ago.
I am using sed in bash shell script. I want to add some text, which is defined as shell variable, in the first place of each line using sed.
For example,
cat filename
1.cfg
2.cfg
...
100.cfg
In the script,
#!/bin/bash
currentd="/home/test/"
sed "/WHAT SHOUL I DO to add $currentd/" filename > filepath
I want to add the file path currentd in the first place of the each line in filename and get filepath file.
sed "s|^|$currentd|" filename > filepath
This "replaces" the start of each line (^) with $currentd. This requires that $currentd not contain any pipes (|).
You can also do it using awk, as:
awk -v c=$currentd '{print c $0}' filename > filepath
-v is used to set awk variable c to bash variable currentd.
This question already has answers here:
Extract lines between two patterns from a file [duplicate]
(3 answers)
Closed 8 years ago.
I have a text file which I want to extract after "String1" and before "String2" using unix shell commands such as grep, awk, sed.
Part of a text file as follows(I need to get after "Specific" and before the second dashed line):
------------------
Specific:
line1
line2
------------------
line3
Based on Extract lines between two patterns from a file:
$ awk '/------------------/ {p=0}; p; /Specific/ {p=1}' file
line1
line2
Quoting my own explanation:
When it finds pattern1, then makes variable p=1.
it just prints lines when p==1. This is accomplished with the p condition. If it is true, it performs the default awk action, that is,
print $0. Otherwise, it does not.
When it finds pattern2, then makes variable p=0. As this condition is checked after p condition, it will print the line in which
pattern2 appears for the first time.
So the difference is that ------------------ line is checked before printing, so that the flag can be stopped. And Specific line is checked after printing, so that the flag is turned on without printing this specific line.
It could also be done using next after setting p=1 so awk jumps to the next line:
awk '/------------------/ {p=0} /Specific/ {p=1; next} p' file