Tagged Questions

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

learn more… | top users | synonyms

0
votes
1answer
423 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
1k 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
65 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
105 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
56 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
291 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
594 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
284 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
117 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
4k 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
2k 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
168 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
1k 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
1k 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
4k 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
61 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
102 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
673 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
625 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
736 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/ { # ...
2
votes
2answers
1k 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
342 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
5k 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
1k 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' ...
2
votes
6answers
5k 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
2k 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
207 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
670 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
230 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
103 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
619 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
53 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
142 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 ...
3
votes
2answers
969 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
135 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 ...
5
votes
1answer
601 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
2answers
1k 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 ...
0
votes
1answer
92 views

Building Linux from Scratch stuck, incompatible sed version? [duplicate]

I am trying to build Linux from Scratch per LFS website While I was able to install all the packages until 6.7.1 without any error, I am stuck at Installation of Linux API headers (This is after I ...
2
votes
3answers
103 views

Pulling out keywords using standard command line utilities?

As an example, this is the pronunciation of "when" according to Wiktionary. enPR, IPA and X-SAMPA are different schemes for showing pronunciations. when:* {{a|US}} {{enPR|wĕn|hwĕn}}, ...
2
votes
3answers
112 views

Expand placeholders with same-named environment vars in text file?

There are a lot of variations on this question, but I wanted to ask it here anyways because I am so close (!) to finding a solution on this one - I just need a little nudge. Scenario I have several ...
5
votes
1answer
222 views

Execute a command within 'sed'

I have a list of random memory addresses saved in a text file, and I need to sed them out, decode them, and save them back to my file. However, the decoding operation I'm trying to perform is handled ...
2
votes
5answers
8k views

Extract a part of one line from a file with sed

I want to read one part of one line from a file. For example: POP3_SERVER_NAME = localhost I want to return only localhost, using sed. This text is on the third line. I do this to extract the ...
3
votes
2answers
2k views

Removing unwanted lines in text file

All I need is the sed or awk code to remove the even numbered lines in a text file. Nothing fancy I just need to remove irrelevant data on the even lines of a text file.
2
votes
2answers
3k views

Comparing two files using Unix and Awk

I need to compare two files, File1 and File2 (Separated by space) using 4 fields (Field 1, 2, 4 and 5 of File 1 with field1, 2, 4 and 5 of File2). Logic: If column 1, 2 and 4 of File 1 matches with ...
3
votes
4answers
388 views

Replace all occurences of \ except \N

I have a file like this: 1,2,subjects,\mat\hs,unix\,\Nato,\N,123,\N I want to replace all occurences of \ with #, except for \N, such that the output looks like this: ...
1
vote
2answers
162 views

Backreferencing with sed

I have a database containing a list of unsigned longs representing a bunch of memory addresses followed by an offset within two brackets (e.g. Mem[offset], 0x2322AD4[3]), and some other data. What I ...
1
vote
2answers
159 views

Tell a regex expression to skip the beginning of a line before matching

If I had a string like 45mb ./aaaa/bbbb/cccc/dddd and I use a regex like [^\/]*\/ it will match 45mb ./aaaa/bbbb/cccc/ I only want it to match ./aaaa/bbbb/cccc/ but I can't figure out how to do ...
1
vote
1answer
89 views

What are the commands that you can use in the beginning of a sed command?

I initially learned to do sed search and replace like so: sed -e 's/<stuff>/<replacement>/' However I recently learned that these will do something different based on what command ...
2
votes
2answers
80 views

No result when using Multiple write file & sed in shell script

I would like to swap variables from old file to new file when I use single line like this cat ../oldFile.html | sed "s@$dayMaxInNew@$dayMaxIn@g" > newFile There's result from newFile. But When ...
1
vote
1answer
59 views

How to use GNU tools to extract and merge two substrings of the same string?

Suppose I get the following result after doing a grep "emailed" /home/John/logs/2013-07-10.171222-0400EDT.html:I have emailed Mark. Thanks I want to extract the date from this so that my output ...