I have a file which has 2000 lines of data (file name is data.tsv). I want to replace the string with empty line where there is a matching pattern, in my case is PMC:
How can I do with Vim or other sed command?
Thanks,
Rio
With vim you can do it like this:
:g/PMC:/normal S
Try this with GNU sed:
sed -i 's/.*PMC:.*//' data.tsv
Another way is:
:%s/.*PMC:.*//
where the '%' means 'every line' and the 's' is subsitute. An alternate:
:g/PMC:/s/.*//
where the 'g/PMC:/' is short for 'global if line contains "PMC:"' and the 's/.*//' means 'match anything and replace it with the empty string'
Related
I have one problem with my sed command. Got string foooooooBaaaar and I need comma separate before Baaaar to be fooooooo,Baaaar. Maybe whom knows how can i do it and can explain me ? Thanks!
To insert a comma before an uppercase letter:
$ sed 's/[A-Z]/,&/' <<< "foooooooBaaaar"
fooooooo,Baaaar
Explanation:
s: substitute
[A-Z]: uppercase letter
with comma followed by the matching letter(&)
To add a comma before Baaaar string:
$ sed 's/Baaaar/,&/' <<< "foooooooBaaaar"
I'm trying find and remove strings like:
[1126604244001,85.00], [1122204245002,85.00], [1221104246003,85.00],
[1222204247004,85.00], [1823304248005,85.00], [1424404249006,85.00],
85.00 = constans. I mean [xxxxxxxxxxxxx,85.00],
In notepad++ is simple:
find: "[^........].............,85.00]" and replace:""
I wolud like to use awk or sed to remove string automaticly without importing it to notepad++.
ok, I have file
temp.txt
[1126604244001,17.00], [1126604244001,17.00], [1126604244001,17.00],
[1126604244001,85.00], [1122204245002,85.00], [1221104246003,85.00],
[1222204247004,85.00], [1823304248005,85.00], [1424404249006,85.00], [1126604244001,17.00], [1126604244001,17.00],
My desire output
temp.txt
[1126604244001,17.00],[1126604244001,17.00],[1126604244001,17.00],[1126604244001,17.00],[1126604244001,17.00],
Thx in advance!
With sed, simply:
sed 's/\[[^]]*,85.00\],[[:space:]]*//g' filename
With this, everything that matches the regex \[[^]]*,85.00\],[[:space:]]* is removed. The regex matches [ followed by an arbitrary number of characters that are not ], followed by ,85.00], and optionally spaces; the only syntactically tricky bit is the [^]] character set which matches all characters other than ].
Alternatively with awk:
awk -v RS='],' -v ORS='],' '!/,85.00$/' filename
This splits the input into records delimited by ], and prints only those that don't end with ,85.00.
egrep -v '[^0-9]85\.00]' YourFile
remove (not empty) line with your pattern
How to insert string to the beginning of the last line?
I want to add a time stamp to a text file which contains multiple lines
var1 = `date`
LINE1
LINE2
LINE3
...
(INSERT var1 here) LASTLINE
sed 's/^/test line /g' textfile inserts characters to the beginning of every line but how can I specifically modify the last line only?
Thanks
Going forward:
sed '$s/^/sample text /' textfile works, but only when inserting regular strings. If I try
var1 = "sample text"
and use substition, here are the problems I encounter
using single quotes in sed does not expand variables, so sed '$s/^/$var1/' textfile will insert the string $var1 into the beginning of the last line.
To enable variable substitution I tried using double quotes. It works when I specify the exact line number. something like:
sed "5s/^/$var1/" textfile
But when I try sed "$s/^/$var1" text file, it returns an error:
sed: -e expression #1, char 5: extra characters after command
Can someone help me please?
Like this:
sed '$s/^/test line /' textfile
$ indicates last line. Similarly, you can insert into a any specific line by putting the line number in place of $
But when I try sed "$s/^/$var1" text file, it returns an error:
It returns an error because the shell attempts to expand $s since you've used double quotes. You need to escape the $ in $s.
sed "\$s/^/$var1/" filename
sedshould be the best tool, but awk can do this too:
awk '{a[++t]=$0} END {for (i=1;i<t;i++) print a[i];print v$0}' v="$var1" file
It will insert value of var1 in front of last line
Another variation
awk 'NR==f {$0=v$0}1' v="$var1" f=$(wc -l file)
PS you do not need to specify file after awk, not sure why. If you do so, it reads it double.
This command would work for you:
sed -i "5s/^/$var1 /" text file
How can I replace a string in a file if line starts with another string using sed?
For example, replace this line:
connection = sqlite://keystone:[YOURPASSWORD]#[YOURIP]/keystone
With this line:
connection = mysql://keystone:password#10.1.1.10/keystone
Answer:
sed '/^start_string/s/search_string/replace_string/'
Information at http://www.gnu.org/software/sed/manual/sed.html#Addresses
If you want to change an entire line which starts with a pattern and ends with enything else, you can use command c description Example
sed -i '/^connection = sqlite/c\connection = mysql://keystone:password#10.1.1.10/keystone' your_file
You can do simply this :
sed -ri 's/sqlite/mysql/g' YOURFILE
sed '/^string1/ { s,string2,string3, }' file
This will replace string2 with string3 on all the lines that start with string1.
My Stress.k file is as follows
180.4430
*INCLUDE
$# filename
*STRESS_INITIALIZATION
*END
I want it to be like
180.4430
*INCLUDE
$# filename
*STRESS_INITIALIZATION
*/home/hassan/534.k
*END
for that I used sed as follows
a="$(cat flow.k)"
sed -i -e '/*END/i \*/home/hassan/$a.k ' Stress.k
where flow.k has only a single number like 534.k or something . Here sed put the line before END but it doesn't take the value of a , instead it puts the same alphabet and it doesn't understand $a.k.
Please also tell me how to delete the second last line or the line with a string hassan for example so that I can delete it first and the for the next step I use it to enter my required line.
if possible please also suggest the alternatives.
best regards
bash variables are only replaced when in double quotes, e.g.
sed -i -e "/*END/i \*/home/hassan/$a.k " Stress.k
Use double quotes to allow the variable to be expanded.
sed -i -e "/*END/i \*/home/hassan/$a.k " Stress.k
To replace the string, do it as you read in the file:
a=$(sed 's/534/100/' flow.k)
To delete a line:
sed '/hassan/d' inputfile
To read a file into the stream after the current line:
sed '/foo/r filename' inputfile