sed is a command-line stream editor for filtering and transforming text.

learn more… | top users | synonyms

3
votes
5answers
157 views

How to add column in the beginning of file using perl?

I want a Perl one-liner that checks whether the first fields of an input file is the file's name and, if it isn't, adds the file name as the first column on every line. Example written in shell : ...
8
votes
3answers
446 views

sed - how to capitalize every 3rd word?

Given: main_east_library main_west_roof main_north_roof minor_south_roof How can I used sed (specifically, not awk, tr, etc.) to create: main_east_Library main_west_Roof main_north_Roof ...
9
votes
4answers
6k views

Concatenate lines by first column by awk or sed

How can I use awk in the following situation? I want to concatenate lines that start with the same column. Only the first column is kept after the join (in this case aaa, www, hhh). The file may be ...
8
votes
1answer
213 views

What is the point of using multiple exclamation marks in sed?

POSIX sed documentation said: A function can be preceded by one or more '!' characters, in which case the function shall be applied if the addresses do not select the pattern space. Zero or ...
6
votes
1answer
115 views

How can I extract/change lines in a text file whose data are separated into fields?

How can I manipulate field-based data from the commandline? For example How can I print only lines whose Nth field is foo? How can I print only lines whose Nth field isn't foo? How can I print only ...
-1
votes
1answer
36 views

Using sed to replace a string with the last character unspecified?

I want to use sed (or any method) where I replace a string in a file. The string is of the format "prv=x" where x is some integer. Is there a way in sed that I can designate this entire string to be ...
7
votes
7answers
403 views

Removing numeric values in certain columns whilst keeping minus signs?

I put my hands up to being a complete beginner, so apologies if I'm doing this wrong. Say I have the following data frame that continues indefinitely horizontally and vertically with negative numbers ...
-3
votes
2answers
43 views

sed or awk to get a string

I have a shell script with string=<deploymentTargets xmi:type="appdeployment:ClusteredTarget" xmi:id="ClusteredTarget_143378365 7353" name="cluster1"/> I want the value between name=" and ...
1
vote
1answer
23 views

calculate numbers on end and merge to one line

So in a file i have lines like these: thisdata:thisother:1337 thisdata:thisother:1800 thisdata:thisother:1500 thisdata:thisother:46984 I would like to output: thisdata:thisother:51621 for all ...
-1
votes
1answer
19 views

Matching 2 different files by first column

So I have 2 different files. file1.txt is: abcdefjeiireiiioe:data:otherdata rijirjeiwrieeoooe:datamore:otherdatamore file2.txt is: abcdefjeiireiiioe:data:otherotherdata ...
5
votes
1answer
124 views

Why doesn't sed exit immediately after writing the output?

I ran sed on a large file, and used the pv utility to see how quickly it's reading input and writing output. Although pv showed that sed read the input and wrote the output within about 5 seconds, sed ...
2
votes
4answers
68 views

Sum of alternate values in a column using either sed or nawk

foo.txt: 1 10 11 2 20 22 3 30 32 4 40 42 5 50 52 6 60 62 7 70 72 8 80 82 9 90 92 10 100 110 Desired Out.txt : 1 10 11 2 20 22 3 ...
-1
votes
1answer
37 views

Why sed replace all occurences instead of first occurence? [duplicate]

I'm trying to replace the first occurrence in a file using sed: sed -i s/he/He/ dummy.txt Instead of replacing first occurrence, it replaces all the occurrences, even without /g. According to ...
0
votes
1answer
60 views

Convert particular lines in a text file to bold [closed]

I have a text file with 100 lines in it. I want to convert particular lines, lets say line1, line6 , line27, line 48, line 86, line99 to bold. I am using solaris 5.10, with KSH. Is there a way I can ...
0
votes
5answers
100 views

Print everything between two patterns, then delete first and last line of the resulting output

otherdata otherdata start_data one two three four end_data otherdata otherdata The resulting output should just be: one two three four This looked like a job for sed to me: sed -n ...
0
votes
2answers
29 views

Sed to keep header(s) line(s) + matching lines

I have not found (nor been able to guess) a sed command that would allow me : To keep : specific lines according to their index (the header for instance) To keep : lines matching a pattern (like ...
0
votes
4answers
63 views

How to remove 0 from the second column in a file

I ahve a script which fetches user name and mobile from a file in following format **Current Format** username mobile zzz 03333012345 123 03212334343 245 ...
4
votes
3answers
217 views

How to insert multiple blank lines after every line

I want to insert 5 blank lines after every line in my input file. foo.txt: line 1 line 2 line 3 out.txt: line 1 line 2 line 3 ... Solaris 5.10, nawk or sed.
1
vote
2answers
60 views

Keep line that contains X within the first two fields

I have a .txt converted from .csv that looks like this Smith","example","example","example","example" example","Smith","example","example","example" example","Smith","example","Smith","example" ...
1
vote
3answers
68 views

Delete lines that contain X in specific column .csv [duplicate]

I currently use the following to delete all emails that contain EXAMPLE sed -i '/EXAMPLE/d' newname.csv However that deletes lines that have EXAMPLE in any of the columns. I want it to only delete ...
4
votes
4answers
564 views

How to insert all lines from one file to another file?

File1: .tid.setnr := 1123 .tid.setnr := 3345 .tid.setnr := 5431 .tid.setnr := 89323 File2: .tid.info := 12 .tid.info := 3 .tid.info := 44 .tid.info := 60 Output file: .tid.info := 12 ...
3
votes
6answers
164 views

Case-insensitive count duplicate line, remove duplicate by choose case with highest duplicate

The duplicate is combination of different case text. I need to count number of duplicate (case-insensitive) and then I need to remove duplicate by choose case with highest duplicate. Below example: ...
1
vote
5answers
103 views

Only keep .csv column containing X Y Z data?

How can I keep .csv line data that only contains the words dog or cat and only check for them on the second column, if the word "dog" appears on any other column but not the 2nd then it should be ...
1
vote
2answers
27 views

How to truncate a HTML link?

INPUT: <a href="docs/2015-05-foobar/foobar.sh">foobar.sh</a> OUTPUT <a href="foobar.sh">foobar.sh</a> Question: How can I remove the "docs/2015-05-foobar/" ? The string ...
0
votes
2answers
29 views

Unable to perform a simple multiplication in script

I am havingf trouble performing a simple multiplication in my script. while read A B C do tmp=$A\*$C/100 echo $tmp >> out1.txt done < foo.txt foo.txt: 13721725 99 100 ...
51
votes
3answers
93k views

What characters do I need to escape when using sed in a sh script?

Take the following script: #!/bin/sh sed 's/(127\.0\.1\.1)\s/\1/' [some file] If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't ...
2
votes
4answers
127 views

Change text in tag, but only tag is contained within a certain XML block

Using Git Bash, I'm trying to conditionally replace what's in the yrot tag in hundreds of files, but only if it belongs with a part name tag that pertains to wheel. // YES, change <part ...
2
votes
5answers
126 views

Adjust gap between 2 columns to make them look straight

file1.txt: hi wonderful amazing sorry superman superhumanwith loss file2.txt : 1 2 3 4 5 6 7 When i try to combine using paste -d" " file1.txt file2.txt > actualout.txt actualout.txt : hi 1 ...
6
votes
2answers
59k views

Using 'sed' to find and replace [duplicate]

I know this question has probably been answered before. I have seen many threads about this in various places, but the answers are usually hard to extract for me. I am looking for help with an ...
0
votes
2answers
49 views

How to find and replace only a particular word (which is at the end of the file) using shell command?

File1: Dms 01.01.2015 feeder1 6kv close at 04:30 Updated Dms 01.01.2015 feeder1 6kv open at 06:20 Updated Dms 04.02.2015 feeder10 6kv close at 17:23 Updated Dms 04.02.2015 feeder12 6kv ...
-2
votes
1answer
54 views

Remove everything else after this mark?

Data C. Induction anesthesia, muscle relaxant, anxiolytics, 1st line sleeping disorder (but short time in market). \part{Theoretical Questions} Lorem Code cat ...
1
vote
3answers
46 views

How to grep between words in a log?

I have a txt file with lines like below. Starting With: Parameters: {"raw_message"=>"MSH....... Ending With: </HL7Message>"} I'm wanting to grep and output to a file the words between ...
1
vote
5answers
111 views

How to remove a string of characters after and before a specific character?

I have a file with thousands of lines that start with: >Miriam132_38138 Otu32555|1 I need to remove 'Miriam*********' so that each lines begins with: >Otu32555|1 The first string of ...
5
votes
3answers
3k views

sed: multi-line replace of config block

I have some configuration files that basically look like (...content...) # BEGIN DYNAMIC BLOCK - DO NOT EDIT MANUALLY (... more content ...) # END DYNAMIC BLOCK (... even more content ...) Now, in ...
5
votes
8answers
1k views

How do I delete the first n lines and last line of a file using shell commands?

I have a file named Element_query containing the result of a query : SQL> select count (*) from element; [Output of the query which I want to keep in my file] SQL> spool off; I want to ...
1
vote
1answer
64 views

How to write data one by one from tmp files to the final output file using for loop?

I have two tmp files named tmp1 and tmp2 which contains some lines. tmp1 file, 1c\ datafile no. 23 2c\ datafile is ok tmp2 file, 3c\ datafile no. 24 4c\ datafile is ok I have a ...
0
votes
1answer
40 views

How to remove certain number of lines every time particular string is found in file

I want to remove 1 previous line and 10 following lines every time a string was found in the file. foo.txt: } Name: john Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 Line 11 Line 12 ...
4
votes
2answers
108 views

Replace 1st with 1), 2nd with 2), … in GNU Sed

Pseudocode which is a continuation from this answer gsed 's/|/1)/g' 's/|/2)/2g' 's/|/3)/3g' 's/|/5)/4g' 's/|/5)/5g' <input.csv >output.csv which is of course not working. I am interested in ...
1
vote
2answers
56 views

How to print few lines from middle of a file using unix shell commands? [closed]

My file containing the following lines, $BQ { VOL @home } database daba relation tcdeatid opendb clear .lruno := 72 <-- (This line lruno. := 72 has 10 times ...
2
votes
3answers
40 views

Truncating the precision of a date

I'm working with a bash script which polls build progress from several different systems. If a build is complete, it stats a log file to find out the last time it was edited, then feeds that time into ...
1
vote
3answers
54 views

Search word preceded by Name in Sed

This thread is motivated by the arithmetic command found in the link of this thread here in Sed. So I would like to do backward arithmetic in Sed. Data Mikael symptom David symptom hello symptom ...
1
vote
2answers
27 views

Addition of values

foo.txt: Lillypaul_sg 204803 204803 204803 Ammy_sg 3 ramaswamy_sg 3 33 tommy_sg 3 3 137374 100 Desired Out.txt : Lillypaul_sg 614409 Ammy_sg 3 ramaswamy_sg 36 tommy_sg 137480 The output is a sum ...
1
vote
3answers
97 views

Sed Replace at a specific group of positions

I have a sample input file as below: Apple Orange Gold Silver Spoon Apple Orange Gold Apple Apple Orange Apple Orange Mango Gold ...
0
votes
1answer
49 views

seeking hints for correct escaping of sed commands WITHIN yaml

I am having a hard time finding the issue why following lines of "code" break my cloud-init config. Basically I am calling a son object as part of the DigitalOcean API which has a YAML format - I got ...
14
votes
10answers
18k views

How to print all lines after a match up to the end of the file?

Input file1 is: dog 123 4335 cat 13123 23424 deer 2131 213132 bear 2313 21313 I give the match the pattern from in other file ( like dog 123 4335 from file2). I match the pattern of the line ...
3
votes
2answers
95 views

shell script to extract between characters

I would like to extract a user name from the file between the keywords "/" and "NET" like below. How can I do so with programs like awk, sed or cut. from: audit: command=true ...
3
votes
4answers
257 views

Extract paragraph separated with *** using AWK

I have a file like below: blablabla blablabla *** thingsIwantToRead1 thingsIwantToRead2 thingsIwantToRead3 blablabla blablabla I want to extract the paragraph with thingsIwantToRead. When I had to ...
24
votes
6answers
58k views

Delete First line of a file

How can I delete the first line of a file and keep the changes? I tried this but it erases the whole content of the file. $sed 1d file.txt > file.txt
3
votes
2answers
923 views

Split log file by time range

I have a log file and would like to cut it into copies of 15min. In the log file the lines start with: dd mmm yyyy hh:mm:ss,xxx e.g. 12 Feb 2013 16:05:02,xxx log text... and the file rotates ...
-2
votes
4answers
71 views

How to print last few contents in a line?

I have a file that has lines shown below, ta=/*92/*39/*1/*649/*12 ta=/*92/*12/*4/*342/*90 ta=/*92/*33/*9/*455/*3 ta=/*92/*12/*3/*649/*25 ta=/*92/*99/*7/*649/*7 and so on. I want a file that will ...