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

learn more… | top users | synonyms

1
vote
1answer
164 views

How to use awk or sed to convert csv diffs into more readable format

Can anyone give me the example of how to use awk or sed (not sure which one, as I haven't use either of these much, as I have mostly been using grep and cut to work with csv data) to convert ...
2
votes
3answers
386 views

Printing match groups with grep

I have the following grep: grep -Po 'href="(http://.*)">Transcript.* - ([0-9]*)\. (.*)</a>' I would like to print out the match groups. Is there a way to do this without Perl/Python? I ...
2
votes
2answers
108 views

Sanitize Apache access log files?

We have this code in a shell script that pipes output for Apache to log. declare -a values=( $taintRequestVals ) for item in ${!values[@]} do cat $apacheLog | sed ...
2
votes
1answer
42 views

How can i change the format of text using sed?

cat > inputfile X1 a1,b1,c1,d1.................. dn X2 a2,b2,c2,d2.................. and so on upto n ................................................ ...
12
votes
8answers
797 views

How to perform a multi line grep

How would you perform a grep for text that appears on two lines? For example: pbsnodes is a command I use that returns the utilization of a linux cluster root$ pbsnodes node1 state = free ...
10
votes
5answers
275 views

How to achieve portability with sed -i (in-place editing)?

I'm writing shell scripts for my server, which is a shared hosting running FreeBSD. I also want to be able to test them locally, on my PC running Linux. Hence, I'm trying to write them in a portable ...
3
votes
2answers
133 views

Join: Two files - but only append last two columns

Given the files: 1.txt 1, abc, 123, 456, 789 2, lmn, 123, 456, 789 3, pqr, 123, 456, 789 2.txt 1, abc, 123, 000, 000 3, lmn, 123, 000, 000 9, opq, 123, 000, 000 OUTPUT.txt ID, NAME, X, ...
5
votes
2answers
905 views

Single command taking two strings to extract string between them, like 'tr' (without expressions)

Saw here a way to use sed to get text between two other strings in a line, like: sed 's/.*starting_text\(.*\)ending_text.*/\1/' but I'd like a simple command (like tr, but for string extraction) ...
3
votes
3answers
124 views

Join every other column with sed or awk

I have a large text file (666000 colums) in the format A B C D E F Desired output AB CD EF How can we do it in sed or awk. I have tried a couple of things but nothing seems to be working. Please ...
6
votes
5answers
221 views

Get filesystem name and usage with `df -h`

df -h | grep test | sed -e 's/*%.^ //g;s/.*[ ^I]//' Output: /test /test/drv0 /test/drv1 df -h | grep test | cut -d '%' -f1 | sed -e 's/*%.^ //g;s/.*[ ^I]//' Output: 10 20 15 I want to ...
3
votes
1answer
458 views

Get all regex matches between two patterns and print them to file

I've got a file with a bunch of long lines. I'd like to grab every group between two patterns and print them to a new file, one match per line. I could manage to do this with Python, but I'd prefer ...
0
votes
2answers
274 views

How to remove n number of lines from a file using sed command? [duplicate]

I want to remove a particular number of lines after the given pattern is matched. Can anyone please tell me how that can be done using the sed command ?
2
votes
4answers
134 views

AWK Search massive file and write to variable name

I want to pull data that I will need to reformat from a simulation file I am working with to prepare it as input for running other simulations. The target data is an optimized chemical z-matrix. The ...
0
votes
1answer
205 views

Sed - unknown option to `s'

I execute a sed command in my script: sed -i "${1}s/${2}=.*/${2}=${REPLY}/" $3 this is the echo: sed -i 13s/log4j.appender.ROOT2.File=.*/log4j.appender.ROOT2.File=/app/TET/log.log/ ...
1
vote
1answer
441 views

How to find out the content of a XML file using Unix Sed/Awk? [duplicate]

I have a XML file(MyXML.xml) like this : <?xml version="1.0" encoding="UTF-8"?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> ...
1
vote
1answer
43 views

How to match a literal '*' with sed?

I have this line in my file called test.php 'dbs_password' => 'a8b*cyP0', and want to replace into: 'dbs_password' => 'password-here', So I use this sed command with syntax: $ sed -e ...
1
vote
2answers
84 views

Sed script inserting and appending in wrong place only in one particular place

I am trying to convert man pages to tex, and I keep getting \item[ in the beginning of the file and ] \hfill \\ After the part of the man pages where it shows ENV(1L). Everything else seems to ...
1
vote
3answers
53 views

Stuck in separating chars into strings in my approach. Found a working approach but can not understand it

I am trying to figure out how to break a string to be on character per line. I.e ahebhaaa to be: a h e b h a a a I tried: $ echo ahebhaaa | sed 's/\(.\)\(.\)/\1\n\2/g' I.e. my ...
4
votes
2answers
153 views

Why do I need to escape regex characters in sed to be interpreted as regex characters?

It seems e.g. cat sed_data.txt | sed 's/\b[0-9]\{3\}\b/NUMBER/g' that I must escape characters to form a regular expression. In this case I had to escape braces in order to be interpreted as a number ...
1
vote
2answers
239 views

Improve sed command to replace first instance of character and all following characters?

Input: <e1 name="file1" id="id1" anotherId="id2"> Desired output: file1 I can get what I need with this: echo '<e1 name="file1" id="id1" anotherId="id2">' | sed ...
0
votes
1answer
115 views

Assign sed result to a variable

I have a sed command and I would like to assign the result to a variable. My command is: sed -n 's/^Release:\s*\(.*\){?dist}/\1/p' file.spec And I tried var = $(sed -n ...
2
votes
3answers
87 views

Using sed to insert latex commands around headers of a document

I have a documen with headers in ALL CAPS, for example: NAME env - run a program in a modified environment and I want to insert \item[ before the header and ] \hfill \\ after a header ...
1
vote
4answers
781 views

How to extract line from the file on specific condition

File: chromosome position ref alt chr1 1398 A T chr1 2980 A C chr2 3323 C T,A chr2 3749 T G chr3 5251 C T,G chr3 ...
2
votes
5answers
823 views

How to remove duplicate letters using sed?

Using sed, how can I remove duplicate letters from HEADERS within a text file? NNAAMMEE nice - run a program with modified scheduling priority SSYYNNOOPPSSIISS nice [-n ...
3
votes
1answer
127 views

How can I write a sed script to delete numbers from a line?

Sample File a.b.gg3d 0000000001 0000000001 y a.b.112a 0000000000 0000000001 y a.b.gg3s 0000000001 0000000001 y a.b.1111 0000000000 0000000001 m I have a file that includes about three thousand ...
2
votes
2answers
387 views

How to remove an apostrophe ( ' ) from couple of columns of a .CSV file?

I have a .CSV file with 7 fields, and the 3rd and 4th columns of the file has a number starting with an apostrophe ( ' ). Please see the example below. col0,col1,col2,col3,col4,col5,col6, ...
1
vote
2answers
206 views

sed : Ignore line starting whitespace for match

I want to replace the default listening port of httpd to 9090. I can edit the line in httpd.conf file using below sed -i "/^Listen/c\Listen 9090" /etc/httpd/conf/httpd.conf But the line Listen 80 ...
5
votes
4answers
993 views

SED : Delete 4 line above and below 5 line after pattern match

I have Hugh file with following details : define host{ use generic-host ; Name of host template to use host_name ServerA_172.29.16.102 ...
1
vote
1answer
49 views

Problem with sed

root@ROUTER:~# cat maccheck.txt logread | egrep ': STA |DHCPACK' | awk '{print $1" "$2" "$3" "$9}' | sed -e 's/\( [0-9] \)/0\1/' | sed "s/.\{15\}/&:/; s/: /:/g" | cut -d : -f 1,2,4,5,6,7,8,9 | ...
4
votes
6answers
96 views

Only show the lines that are in all textfiles at least once

a.txt cat a.txt a b x c b.txt cat b.txt d e a f the q: SOMEMAGICK *.txt a Q: how can I show only the lines that are in all *.txt files?
4
votes
1answer
301 views

Unix - How to append text to every line of a file except for the header line/first line

I have a file say abc.txt with following data (real data has many records,say around 200) Sno | Name 1 | Jack 2 | Jill 3 | June Now how do I append text to make my file look like this, ...
2
votes
3answers
216 views

sed - find string and append

Suppose you have the following text file 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 ...
4
votes
2answers
297 views

Using sed to delete everything between two words?

This doesn't seem to work The next example would delete everything between "ONE" and "TWO:" #!/bin/sh sed ' /ONE/ { # append a line N # search for TWO on the second line /\n.*TWO/ { # ...
1
vote
2answers
533 views

Why `sed` does not return exit status if regex does not matched ?

I'm trying to search and replace some line from files, but when I run sed if that regex not match then sed does not return any status code, So instead of adding one more condition with grep, is there ...
0
votes
2answers
213 views

sed with auto escape character [duplicate]

CommentAppend() { # Comment line and append line below commented line local comment="$1" # search this line and comment it local append="$2" # Append this line below commented ...
3
votes
3answers
2k views

importing data from a text file to a bash script

I need to import a text file like this below: AMBI CBNK CCXI CERE CLACW CNYD DAEG DLIA DLLR DNDN DSGX HAST HIBB, HPOL IRDMZ MARK NEPT NFEC NINE NRCIB OMER PLCC PLPM PSUN UNTK There are 25 entries, ...
3
votes
2answers
405 views

How to run a remote sed command through ssh if the data includes double quotes (“”)?

I'm trying to run through a list of servers and run the next command: itaig@itaig-lt:~$ ssh root@$srvname "sed -i 's/VNCSERVERARGS[6]="-geometry 1920x1080"/VNCSERVERARGS[6]="-geometry 1152x864"/g' ...
0
votes
6answers
1k views

Using cut/awk/sed with two different delimiters

I have the following cases: case@test.com case_1_2@test.com case_1@test.com I'm trying to convert these to case@test.com case@test.com case@test.com So it should remove everything from ...
2
votes
3answers
442 views

Using a reference to a bash string variable in sed

I'm a relative Linux novice. Suppose that I have a text file a.txt that contains the following text: A B C Now, if I want to change the text on line 2 (which contains B), I can use the following ...
1
vote
2answers
134 views

Why the inconsistency with using cat vs. echo piped to this sed command?

Inspired by this question: sed: N command does not read single line I wanted to understand why there appears to be an inconsistency between the following 2 commands: #1. echo This command produces ...
1
vote
1answer
182 views

sed: N command does not read single line

Why sed's N command does not read single line? # single line without \n $ echo -en 'abc' | sed -n 'N;p' $ # I expected 'abc' # single line with \n $ echo -en 'abc\n' | sed -n 'N;p' $ # I expected ...
3
votes
3answers
114 views

Easily get a particular column from output without sed or awk

Is there a quicker way of getting a couple of column of values than futzing with sed and awk? For instance, if I have the output of ls -hal / and I want to just get the file and directory names and ...
1
vote
4answers
80 views

Sed not giving the first occurence of pattern

cat file contains inet addr:192.168.154.102 Bcast:192.168.154.255 Mask:255.255.255.0 inet addr:127.0.0.1 Mask:255.0.0.0 i am extracting the ipaddress(192.168.154.102 and 127.0.0.1) using sed like ...
1
vote
3answers
270 views

How to limit MAX an MIN length of a string?

"nothing" means no OUTPUT. Q: how can I limit the MIN and MAX length of a piped STRING? (MIN: x>=3 chars, MAX: x<=32 chars) INPUT: echo 12 | MAGIC OUTPUT: nothing INPUT: echo 123 | ...
1
vote
1answer
50 views

How to make URL's out of this text with least resources?

INPUT: nyeeeeeeeeeeeeeee.txt:sadfsadf sadjflsajfd:jasdlfjalsfd foo/asdfsafd.txt:laksjdlfajsfd OUTPUT: <a href="nyeeeeeeeeeeeeeee.txt">nyeeeeeeeeeeeeeee.txt</a>:sadfsadf <a ...
1
vote
1answer
110 views

Searching for all the array elements in another array?

This is how I would search for an element in an array. count=${#ARRAY[@]} lsearch() { status=-1 for((i=0;i<count;i++)) do Temp=$1 if [[ $Temp -eq ${ARRAY[i]} ]] then ...
1
vote
2answers
155 views

Print a text file from a line number to another after searching for the lines

I want to print a text file from X line number to Y line number The value of X & Y is determined by searching for a specific line in the text file(in this case the page number) I search for the ...
1
vote
2answers
111 views

Remove 2 and 3 newlines in a text file but not 1

I'm developing on a max osx 10.8 and have no preferences to which tool to use probably sed but it doesn't matter. Update after comment: The input file uses windows new lines if that's complicates ...
4
votes
1answer
178 views

grep: display filename once, then display context with line numbers

Our source code has error codes scattered throughout. Finding them is easy with grep, but I'd like a bash function find_code that I can execute (eg. find_code ####) which will provide output along ...
2
votes
1answer
375 views

Some UTF-8 characters not being recognized by grep or sed

Trying to determine all characters in a file. The file sample consists of: a eɪ abandon əˈbændən ability əˈbɪləti able ˈeɪbəl able ˈeɪbl abortion əˈbɔrʃən abortion əˈbɔrʃn about əˈbaʊt above əˈbʌv ...