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

learn more… | top users | synonyms

1
vote
1answer
74 views

Remove commented lines except one comment using sed

Not very familiar with complex usages of sed, but I have this requirement now. I have a file 1.txt with contents in the following pattern: sree@amby01:/home/sree : cat 1.txt SSL this first line #...
2
votes
1answer
145 views

Read from and append to file at the same time while preserving end-of-line

Conceptually I have an easy task... I have [loosely] structured data in a file: Testing: debug, default CXXFLAGS <100's of additional output lines> Testing: release, default CXXFLAGS <100's ...
1
vote
4answers
56 views

Getting substrings in between delimiters multiple times from one string without looping

I have looked all over for a solution to this, but I couldn't find one. I am using qstat -x to pass a large string of job information. The output from qstat -x is in XML format. The substrings I am ...
4
votes
4answers
319 views

How to print lines between pattern1 and 2nd match of pattern2?

Test file is given below: PATTERN1 a b c PATTERN2 d e f PATTERN2 g h I want to print line between PATTERN1 and 2nd match of PATTERN2: PATTERN1 a b c PATTERN2 d e f PATTERN2
4
votes
5answers
2k views

Remove lines based on pattern but keeping first n lines that match

I need to remove lines from a text file based on pattern but I need to keep the first n lines that match the pattern. Input % 1 % 2 % 3 % 4 % 5 text1 text2 text3 output %1 %2 text1 text2 text3 ...
4
votes
2answers
82 views

How Do I Remove Duplicate Words With Suffixes?

How do I compare a list of unsorted words of variable length and remove duplicate words that have a suffix? Example word list: iron curl curled railroad curler ...
1
vote
3answers
72 views

Generate Sequence number for each line at the end .. modified t he post can someone help&suggest on this

I am new to linux I have got this query sed '/./=' abc.txt| sed '/./N; s/\n/, /' >> as.dat source file has 3 rows like below a b c When use the following command it gives me result like ...
0
votes
1answer
42 views

How can I remove duplicate entries in linux log

I've turned mysql general log on and I've been logging every transaction that occurs for the last week. I also have a cronjob running every 5 minutes doing some mysql transactions which is polluting ...
0
votes
1answer
56 views

problem with sed inside sed

I am trying to parse a string inside a sed command but I am getting errors. sed -e "/pd begin $(echo 3:1I:1:1|sed 0,/:/{s/:/' '/})",/pd end $(echo 3:1I:1:1|sed 0,/:/{s/:/' '/})/p /tmp/hp-raid-data-...
2
votes
1answer
45 views

sed unterminated `s' command [on hold]

I'm getting: sed: -e expression #1, char 46: unterminated `s' command On this code: sed -i.bak -c 's|net.ipv4.ip_forward.*|net.ipv4.ip_forward = 1|' /etc/sysctl.conf My editor is showing that ...
3
votes
2answers
115 views

Add a line below each line matching a pattern only if not already present

Can sed add a new line below a specific content, if the input content exist then leave it ? Current content of the file ssss Hostname example.com Os version rhel5.6 apache 4.2 Hostname example2.com ...
1
vote
2answers
8k views

How to remove a column or multiple columns from file using shell command?

My file, ARCHIVE B1_NAME B2_NAME B3_NAME ELEMENT INFO_NAM WERT PROCID -------- -------- -------- -------- -------- -------- ---- ------ ...
2
votes
2answers
70 views

Retrieving five random items from a list using sed

How can I retrieve randomly selected five different items from a list using sed? My list is something like below. Each item begins with an asterisk at the beginning of a line. * asterisk* star ...
0
votes
1answer
44 views

when i used sed -n ,why this err about sed -n ' $j,4800p' [duplicate]

LINEnum=$(grep -nr "#32" IM_DlCtrlRef.txt | cut -d : -f 1) for j in $LINEnum do echo $j sed -n '$j,4800p' IM_DlCtrlRef.txt >> IM_DlCtrlRef_bak for insertl in {1..4} do ...
1
vote
3answers
44 views

Reformatting negative numbers with sed or awk

I have a text file that contains some negative numbers except, they are formatted as: 00000012- With the hyphen after the number. I am processing the file using awk to reformat it into SQL insert ...
2
votes
2answers
25 views

less combined with sed color sequences prevents page breaks

If I use the command below which colors the lines output in yellow, it essentially turns less into cat, printing out the entire file and disabling all of the features I need with less, such as ...
0
votes
1answer
53 views

Using multiple sed commands for text manipulation

I have a bash file, in which I would like to make several changes. sed -i 's/ / /g' testfile sed -i -e 's/#[a-z]$//' -e '/^$/d' testfile sed -i 's/ *$//' testfile sed '/##=/ r header' ...
1
vote
1answer
177 views

How to sed -i '`[echo $var]`i word' file?

Code: pos=2; printf "Masi \nwas \nhere" > /tmp/1 sed -i '`echo $pos`i huhu' /tmp/1 Expected output: Masi huhu was here Current output: sed: -e expression #1, char 1: unknown command: ``'
0
votes
2answers
46 views

How to add a column of numbers to a array data file

I have a data file with two columns of numbers: input file 4.182 4.1843 4.184 4.2648 4.2281 4.0819 4.2204 4.1676 4.0482 4.1683 4.0156 4.2895 4.4504 5.2369 4.3776 4.4979 4.3797 4.1372 4....
2
votes
2answers
108 views

sed - find correct color sequence for ANSI extended 256 colors

I am currently using the following command to use the standard ANSI colors with the tail command: tail -f syslogfile | sed -e 's/\(.*MAJOR.*\)/\o033[93m\1\o033[39m/' This is bright yellow however I ...
0
votes
1answer
26 views

How to multiply two columns in a file by a constant number

I have a file like this. input data 4.2394 4.4569 4.2427 4.1011 4.2879 4.1237 4.2106 4.4844 4.2373 4.1071 4.1322 4.0502 4.3103 4.4255 4.4342 4.5262 I need to multiply each element by a constant ...
3
votes
3answers
41 views

awk to cut portion of a field and still print entire line

I have this line: 08:30:02.480507 IP 192.168.100.150.65119 > 192.168.100.151:53 59865 [1au] A? click.em.redbox.com. (48) I need to cut out the ".480507" in the timestamp so the line looks like ...
1
vote
3answers
84 views

Find and Replace with command line

my log.txt file includes ... mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 ... how can I replace mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 with mynetworks = 127.0....
0
votes
1answer
76 views

Explanation of sed in shell script

I found a snippet of code online that would actually help with creating the frequency of each word in a text file however I would like someone to explain exactly how it is doing so Especially the ...
1
vote
1answer
27 views

How to unwrap paragraphs using sed

I have a text file that contains paragraphs wrapped at 76 characters with an empty line between paragraphs. I'd like to use sed to convert this to a file that has one line per paragraph, no blank ...
2
votes
2answers
104 views

Extracting positive/negative floating-point numbers from a string

I am trying to extract numbers out of some text. Currently I am using the following: echo "2.5 test. test -50.8" | tr '\n' ' ' | sed -e 's/[^0-9.]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' This ...
-2
votes
1answer
50 views

filtering pattern in a file using sed or grep

I'm new to Linux. I'd like to store all the unique usernames present in the extended.log file into a new file using the commands awk, grep, and/or sed. The following are the field-names in my file, ...
0
votes
0answers
54 views

Extract text above exception line in output [closed]

I have the following error output from a program: 2016-06-20 02:09:28,264 Mbr10063a cOqZeJiM BaseAction.java:getAlerts:618 ERROR [AlertAction] Could not open JDBC Connection for transaction; 2016-06-...
0
votes
0answers
30 views

shell scripting using sed

I want to compare specific string surrounded by alphanumeric and some special chars in file. For example: 5efa4083-7157-437a-ab9a-652ced2fc2ea;: AAA :;5efa4083-7157-437a-ab9a-652ced2fc2ea;BBB I ...
0
votes
4answers
47 views

Find a text in a file and Insert a text after it

I have a text file. The text file contains the below content. ServerName ServerAdmin webmaster@localhost DocumentRoot I need to insert a text www.mydomain.com, after the ServerName. Is there any ...
0
votes
2answers
34 views

Why you cannot have Tab in sed -i “1i \t 'word'” file?

Pseudocode printf "Masi \nwas \nhere" > /tmp/2 sed -i "1i \tword \t 'include word'" /tmp/2 Expected output: tab before the first word. Output: tword 'include word'. Trying to use variable to ...
0
votes
1answer
44 views

Why Blank output of this `sed … <<END_SED | cut …`?

The following sed returns (...| od -c) without ...|cut -d "\n" -f 0000000 1 9 2 \n 2 5 0 \n 0000010 Pseudocode # http://unix.stackexchange.com/a/290407/16920 sed -n -f - /usr/share/...
15
votes
4answers
30k views

How to insert text before the first line of a file?

I've been looking around sed command to add text into a file in a specific line. This works adding text after line 1: sed '1 a\ But I want to add it before line 1. It would be: sed '0 a\ but I ...
3
votes
2answers
352 views

grep: How to find Closing Bracket?

One part of /usr/share/X11/xkb/symbols/us starts with xkb_symbols "dvorak" { and ends with the closing curly bracket }; which line-number I want to find. partial alphanumeric_keys xkb_symbols "...
1
vote
2answers
800 views

Prepend x/ to line where x is a variable on each line

I want to add $x/ from data formatted as $x-xyz (xyz is a mix of text and number to be ignored). Example data AB-10C CD-02E Output AB/AB-10C CD/CD-02E How do I describe the variable as text ...
0
votes
3answers
3k views

How to convert Log file to CSV using bash

Is it possible to turn this log entry into a properly formatted csv file using sed/awk? I've tried multiple ways to do this using sed/awk. Alas I couldn't do it. Before I do it in perl. is this ...
2
votes
1answer
42 views

How can i replace multi line string using sed? [closed]

I have tried the following command:- [root@10 bin]# cat temp asdjahskjdhajksdhjkashjahfksjdhfkjsddhflkdsjhfklsdajhfklajshdkjalsdlkjasdhkljas hskdajkdhkjas cast $recv $UE_CAPABILITY_ENQUIRY ...
-8
votes
2answers
49 views

how to print in a single line with pattern matched

student:xxxxx student:yyyyy student:zzzzz student:kkkkk student:. teacher:aaaaa teacher:bbbbb teacher:lllll teacher:. student:sssss student:mmmmm student:. output should be, student:xxxxx yyyyy ...
8
votes
3answers
8k views

Regex alternation/or operator (foo|bar) in GNU or BSD Sed

I cannot seem to make it work. GNU sed documentation says to escape the pipe, but that doesn't work, nor does using a straight pipe without the escape. Adding parens makes no difference. $ echo '...
0
votes
2answers
316 views

Find line within file matching pattern and make a substitution

I'm trying to simplify my life here but I'm stuck. I have a file containing multiple lines. Each line has a user and a number. I'd like to know a bash one-liner to search for a user and change the ...
25
votes
12answers
12k views

How can I write to the second line of a file from the command line?

I have an external program that produces an output file (largish, 20K lines possible). I need to insert a new line between the existing line 1 and line 2. I've been looking at awk and sed - I use ...
1
vote
2answers
46 views

Specifying comma-separated command line parameters based on text file lines

I want to run a command like: command argument --parameter=param1,param2,param3 where param1, param2, param3 may be any number of parameters coming from a file params.txt, each on one line. I can ...
3
votes
3answers
283 views

sed: what does “/ ” mean?

x=`src/lstat64 $TEST_DIR/$tmp.1 | sed -n -e '/ Links: /s/.*Links: *//p'` In this script. I understand this part "/s/.*Links: *//p'" the only thing I don't understand is '/ Links: ' what does "/ " ...
1
vote
2answers
28 views

Insert text inside stanza at the end of the data string

I have a file I need to echo text into at the end of the file from the command line, the problem is the data is in a stanza, all within curly braces. This is the end of the file: text-string1,text-...
1
vote
2answers
46 views

sed: Grabbing and replacing all data between last backslash and a comma

I have data in a csv format that looks like this: F1309-042543,07/14/09 01:39:25,N/A,C:\windows\system32\netsh.exe,N/A,True F1309-042543,11/21/10 03:24:02,N/A,C:\Windows\System32\networkexplorer.dll,...
3
votes
3answers
120 views

edit repo file with sed

I want to edit manually my repo file from the command-line, preferably using sed. How can I do that based on the repo-name I want to edit? I want to search for a specific repo-name (example: reponame-...
0
votes
3answers
35 views

Escaping regex sed (1 lowercase, 1 uppercase, 1 symbol, 1 number) for bash

The goal is to remove every line not containing one uppercase, one lowercase, one special character and one number from a file. I am unable to run this regex: (?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=....
0
votes
2answers
34 views

How to print lines using find and sed in multiple files?

I've many files in a directories structure. I want to extract some string (namely urls) by using a regexp, from those files. I've tried this : find . -path "*alder/ * / * .html" -print | xargs sed -...
2
votes
2answers
64 views

How to get the last occurrence of lines between two patterns from a file?

I have a log file which reports on the output of a process, I'd like to extract all lines from between the last occurrence of two patterns. The patterns will be along the lines of; Summary process ...
3
votes
3answers
238 views

Use sed with back references

I am trying to remove a space between 2 strings, they are like this: 312.2 MB 123.34 KB 487.1 GB I want to change them to: 312.2MB 123.34KB 487.1GB I've been trying and I can get: echo "312.2 ...