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

learn more… | top users | synonyms

2
votes
3answers
104 views

Call sed to replace a string stored in a bash variable

I want to expand a variable in bash with sed or awk. This variable is an array. For example, the script must delete strings contained in array. I tried ARRAY1=( string1 string2 string3 ) sed -i ...
1
vote
3answers
119 views

sed + find value before word in line

How can I find the value before the word "free"? top -n1 | grep Mem Mem: 2054968k av, 2034120k used, 20848k free, 0k shrd, 186768k buff First I tried top -n1 | grep ...
-2
votes
1answer
49 views

Extract/print every 3rd occurence of pattern

I need help with extracting/printing lines until a stop pattern. This needs to happen after every 3rd occurrence of start pattern, i.e ERROR_FILE_NOT_FOUND and continue until the end of the file. ...
2
votes
1answer
93 views

grep search expression and rename file

I have readable pdf files that all contain the string VAX followed by eight numbers, like VAX00002885. Now I want to find that expression and rename the corresponding pdf to VAX00002885.pdf (in this ...
1
vote
2answers
31 views

char-to-char substituion with sed

I am trying to make a monoalphabetic substitution decoder in bash: I have a set of characters that form an alternative alphabet like real alphabet -> ABCDE ... my alphabet -> DGHJK ... I ...
0
votes
1answer
28 views

Replace copyrights recursively [duplicate]

I have many files under the current directory. Each file contains copyrights headers like, # # Copyright 2013 Company, Inc. All rights reserved. This software # is property of Company, ...
1
vote
1answer
19 views

Using sed to align text starting from pattern to end of line

I have a text like this: LDX LEVEL LDA INITD015,X ; INITIAL ACTIVE SPRITES STA $D015 STA SAVED015 LDA INITXPOS,X ; INITIAL HERO X COORDINATE STA XPOS LDA ...
0
votes
5answers
50 views

df -k + how to match the root volume line

please advise how to match the "/" volume from df -k command? I tried the following: [root@machine /]# df -k | grep "/" /dev/vx/dsk/bootdg/rootvol 130926685 71378710 58238709 56% / /devices ...
0
votes
3answers
47 views

How to remove newlines from an ip a s output?

INPUT ("ip a s" command) 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 ...
1
vote
2answers
43 views

Extracting file name and string from multiple files

I have a lot of files (>100,000) from which I would like to extract a specific string and the filename itself. Files are organized like this: Filename: sequence.php?ID=gi|90022703|ref|YP_528530.1| ...
5
votes
4answers
224 views

Is there a difference between read, head -1, and sed 1q?

The following commands seem to be roughly equivalent: read varname varname=$(head -1) varname=$(sed 1q) One difference is that read is a shell builtin while head and sed aren't. Besides that, is ...
3
votes
2answers
116 views

Extract text including parens

I have some text like this: Sentence #1 (n tokens): Blah Blah Blah [... ... ...] ( #start first set here ... (other possible parens and text here) ) #end first set here (...) (...) Sentence #2 ...
6
votes
1answer
204 views

How do I print all but the Nth to last line in sed?

I'd like to do the complement / "opposite" of sed 13q;d <file.txt More generally, is it possible to do this sort of complement/inverse/opposite in sed? Or only for regexes? How do I print all ...
1
vote
3answers
46 views

Find a string in one section of a file with mutiple sections

How to sed/awk for a string contained in another string. TESTVAR=' { icon : "icons/773_l.png", id : 80, initialState : true, isPng : false, label : "Imagery", opacity : 1, requestType : ...
1
vote
2answers
45 views

Using sed to find a word and replace another word

I have a word like "mode v" in a file. I want to replace it to "mode sv". However all my efforts failed as there is space after mode
0
votes
2answers
31 views

how to remove the apostrophe and delete the space

I want to remove apostrophe from words such as in the word "duck's". I want to remove the apostrophe and the space left behind the apostrophe. I tried sed and I got "duck s" but what I want to get ...
-1
votes
1answer
35 views

search for a character in one column and replace value in another column of matched records

I have a requirement to find value 2 in column 4 and wherever it matches replace 6th column with '-'sign in the beginning suppose if i have csv file - file1 like this abc,0,def,1234,ijk ...
0
votes
2answers
36 views

Add space after 8 characters then add space after every 4 characters

Here is the sample input: AbbigailAbieAbbyAbbi Using sed, I have separated them into 4 characters but seems like I got a problem because I have to separate the first 8 characters first then add ...
2
votes
5answers
124 views

Extract lines from a file containg From <str1> To <str2>

Let's says there is file "abc.txt" that contains a paragraph/lines: Hello, how are you doing sir? when are you going to arrive at the SBAHN platform to catch the train? How can I extract all the ...
8
votes
2answers
276 views

Reindexing a large CSV file

I went through the answers in this helpful thread, but my problem seems to be different enough that I can't think of good answer (at least with sed). I have a large CSV file (200+ GB) with rows that ...
4
votes
3answers
132 views

Add leading zeros until all lines before the comma consist of nine characters and subsequently insert a character every three digits using sed

The aim is to add leading zeros until all lines before the comma consist of nine characters and subsequently insert a character every three digits using sed. Input 12345,1s4c3v6s3nh6 ...
1
vote
0answers
27 views

Create files from templates, substitutions in middle of other words

Is there a way to convince m4 to replace a macro in the middle of a word? I would like this file, day.m4: define(Sat,Cat)dnl Saturday and this command: m4 day.m4 to produce this output: ...
1
vote
3answers
49 views

Replace word instances with its count attached to it

I have a file which contains word 'name' multiple times ( 250 times ). I need to replace every instance of 'name' with its count attached to it as name_1, name_2, name_3....name_250. Can I do this ...
4
votes
6answers
156 views

Sed — Replace first k instances of a word in the file

I want to replace only the first k instances of a word. How can I do this? Eg. Say file foo.txt contains 100 instances occurrences of word 'linux' . I need to replace first 50 occurrences only.
0
votes
4answers
92 views

Use SED or AWK to move rows to columns

I have a report that is generated in rows and I need to move it to columns. The number of rows per record will always be the same. This will be in a BASH script on Linux, so I would like to use ...
1
vote
3answers
46 views

sed match pattern N times

I want to split a file into chunks with 2 words each. $cat tmp word1 word2 word3 word4 word5 word6 word7 $sed -e 's/word. word. /&\n/g' tmp word1 word2 word3 word4 word5 word6 word7 $sed -e ...
2
votes
2answers
117 views

Sed — replace a character in a matched line in place?

In a file containing lines like this one: # lorem ipsum blah variable I would like to remove # (comment) character in the same line that contains a specific string, in place. Is sed good for this? ...
1
vote
1answer
63 views

Sed / awk script for a non-standard apache access log ( Oracle Weblogic )

I have hit a wall with my limited sed scripting abilities, and i wonder if any of you guys could help me out. I have a non-standard apache access log with the following format: #Version: 1.0 ...
3
votes
2answers
84 views

Extract csv files from path to multiple files in bash

I have a files multiple file path for backup purpose ,now i want to filter it based on ".csv " fromat. I have tried this. filesall=(/home/abc/allfiles/*) files=$((${filesall[@]}|grep -F ".csv")) ...
6
votes
2answers
93 views

How come sed is so fast?

I thought about whether this question is suitable for SE or not, I hope you agree it is. Some time ago I asked on SE how to find text in files and leave the file with only the matching lines that ...
2
votes
2answers
37 views

Change path in sql script with sed

I'm trying to change the path in a oracle sql script from a bash script. read -e -p "Enter the ORACLE SID: " -i "orcl" ORACLE_SID_VALUE sed -i "DEFINE tsdir = '/u02/app/oracle/oradata/orcl';/c\DEFINE ...
2
votes
2answers
59 views

Use awk to find first occurrence

I have a log file that gets updated when my script runs. The script will insert "Start of script" text when it starts and "End of script" text when it finishes running. I am trying to capture the ...
2
votes
0answers
38 views

How can I make sed not append a newline character? [duplicate]

How can I make sed not append a newline character to an input stream that was missing one? If I can't, what's the quickest way to remove a newline character from a variable? A code sample was ...
0
votes
3answers
45 views

How to Print certain Fields if a condition is met

I have a big Input file as shown below. If "PRIMER_LEFT_NUM_RETURNED" is not equal to 0 then print the corresponding SEQUENCE_ID, SEQUENCE_TEMPLATE,PRIMER_LEFT_NUM_RETURNED, PRIMER_RIGHT_NUM_RETURNED, ...
5
votes
5answers
103 views

Replace data at specific positions in txt file using data from another file

I have a text file in the below format: $data This is the experimental data good data This is good file datafile 1 4324 3673 6.2e+11 7687 67576 2 3565 8768 8760 5780 8778 "This is line ...
0
votes
4answers
60 views

Adding a character to every other text line

I have a huge (ca. 20G) text file which contains millions of passages (a.k.a. paragraphs) with headers. Headers and paragraphs are always one line each, e.g., Sunshine This is a sunny day. ...
3
votes
3answers
356 views

Replacing string in all files found by grep. Can't get it to work

I'm trying to do what title says. So I find all needed files with grep: grep --include=\*.{php,ini,conf,sh} -ril -P "'([\d\w\-\_\.]+)(@domain.com)'" '/var/www_data/somepath/' Now I assume it's ...
3
votes
4answers
53 views

Need to remove - (Minus) sign at the end in number from many columns and insert it on columns removed

I have a huge csv file which has - (Minus) sign at the end of numbers. Now I need to move these - minus signs to begin of the numbers, i.e I need to remove the last character if it has '-' sign and ...
4
votes
4answers
164 views

extract lines according to a particular column

I have a CSV file like CK,ck XYZ,xyz,xyzs ABC,abc,abcs PQR,pqr,pqrs LMN,lmn, IJK,ijk, I have to check if something is written in column 3 except whitespace, then display the whole line.the output ...
2
votes
7answers
78 views

How to replace the third character in number to other character

IPS parameter include diff IP address as: IPS="192.9.200.12-14 172.17.200.12-89 12.21.1.10-25 127.0.0.1-127" I want to replace the third “.” In the IP address number to “,” How to achieve this by ...
1
vote
1answer
28 views

how to change a complete line with sed c option

The sed command below is supposed to change the country for Sergio Oliva to Cuba from USA, but as you can see in the output that it's still USA. The book I'm using uses this example to illustrate ...
1
vote
2answers
49 views

Using sed with herestring (<<<) and read -a

I use sed with <<< and read to assign all words in a string to variables. What I do is: read -a A0 <<< $(sed '2q;d' /proc/stat) Hence, sed reads the second line of the file file ...
4
votes
3answers
599 views

Replace “o” with “0” in large wordlist and save the original word?

I have a large text file with words. Every word is on a single line (typical wordlist). I want to replace all characters "o" with the number "0" in every word, and the new formed word to be pasted on ...
2
votes
4answers
278 views

Only cat from specific line X (with a pattern) to other specific line Y (with a pattern)

A little extended problem from "cat line x to line y on a huge file": I have a huge file (2-3 GB). I'd like to cat/print only from the line having "foo:" to the line having "goo:". Assume that "foo:" ...
1
vote
1answer
27 views

Cannot redirect output from sed

I'm piping output from clock through sed to remove leading zeroes from numbers. It looks like this: clock -sf 'S%A, %B %d. %I:%M %P' | sed 's/\b0\+\([0-9]\+\)/\1/g' That works fine and produces the ...
5
votes
4answers
631 views

How to add a carriage return before every newline?

I have a file that only uses \n for new lines, but I need it to have \r\n for each new line. How can I do this? For example, I solved it in Vim using :%s/\n/\r\n/g, but I would like to use a script ...
0
votes
0answers
57 views

Can sed and awk take in multiple files?

grep can search in multiple files, including those files recursively under a specified directory. This makes grep able to find which file contains a pattern. Can awk and sed also? It seems that they ...
2
votes
2answers
59 views

Sed find and replace with slashes

I am having trouble using sed find/replace with slashes in the find parameter. Sample contents of test.sh str=$(sed -n '/^user:/p' /tmp/shadowtest) # find "user:" line in /etc/shadowtest ...
2
votes
2answers
34 views

Live replacement, while viewing log using LESS

It's very convenient to monitor logs using less command - you see what is going with your system as it happening. The bad thing is that my logs are optimized for grep tool: each atomic action is ...
1
vote
1answer
34 views

awk: Columns are not printed when concatenating strings is passed as command string

I've found some anomaly when writing a script. The following examples works as expected: $ echo 123 | awk '{print $1 456}' 123456 $ sh -c "echo 123 | awk '{print $1}'" 123 But the following ...