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

learn more… | top users | synonyms

4
votes
5answers
105 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.
1
vote
0answers
17 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: ...
-3
votes
0answers
20 views

How to extract strings from a given input file and store them in two different files in a column wise manner

Given below is the input file pattern: *********************************************************** Search URL: ...
1
vote
3answers
34 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 ...
54
votes
7answers
14k views

Is there a basic tutorial for grep, awk and sed? [closed]

I've been a Linux user for a while, and I've a pretty decent understanding of most the common command line utilities. However, ones that come up and up again in relation to programming are grep, awk, ...
0
votes
4answers
58 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
1answer
12 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 ...
6
votes
2answers
87 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 ...
1
vote
3answers
40 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
93 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? ...
3
votes
1answer
49 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")) ...
2
votes
2answers
31 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
32 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 ...
1
vote
2answers
129 views

Replace columns using a shell

How could I replace column 1 in File1 with column 2 in File2, when a match is found? File1 id1 36987 id2 75685 id3 12345 id4 12896 File2 id1 ID1 id2 ID2 id3 ID3 id5 ID5 I'd like to write this on ...
5
votes
5answers
79 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 ...
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
4answers
46 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. ...
0
votes
3answers
35 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, ...
13
votes
6answers
571 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
3answers
102 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
41 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 ...
2
votes
7answers
73 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 ...
5
votes
4answers
326 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 ...
4
votes
4answers
139 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 ...
30
votes
4answers
2k views

Reverse grepping

Let's say, I have a really big text file (about 10.000.000 lines). I need to grep it from the end and save result to a file. What's the most efficient way to accomplish task?
11
votes
1answer
3k views

How can I replace a string in a file(s)?

Replacing strings in files based on certain search criteria is a very common task. How can I replace string foo with bar in all files in the current directory? do the same recursively for sub ...
1
vote
1answer
24 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 ...
4
votes
3answers
598 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 ...
1
vote
2answers
39 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 ...
2
votes
4answers
242 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
22 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 ...
0
votes
0answers
52 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
41 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
31 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
27 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 ...
0
votes
1answer
45 views

Unix sed interpreter | replace a specified character between two words but only replace if the character has a succeeding specified character also

previously, i posted a question on how to replace a specified character between two words and it has been answered perfectly. It works, but to minimize the effort, i would like to add something on my ...
1
vote
3answers
126 views

Replace specified character between two strings?

i have something to accomplish. i need to replace all occurrence of & inside or between <ex> </ex> to #. actual example below: a & b & c <ex> a & b & c ...
9
votes
2answers
28k views

Return only the portion of a line after a matching pattern

So pulling open a file with cat and then using grep to get matching lines only gets me so far when I am working with the particular log set that I am dealing with. It need a way to match lines to a ...
0
votes
5answers
107 views

print only rows that has latest non empty cell that starts with the word “Failed” on a CSV file in Linux [closed]

I want to print the table only if non-empty latest column that starts with the word "Failed"on a CSV file in Linux.. My input - Name,Mon,Tue,Wed,Thu,Fri,Mon Linux,Successfull Fullbackup; Failed ...
0
votes
1answer
44 views

Is there a way to create a new file and insert text into this file all with bash? [closed]

I need to change and create several configuration files for some kiosks at work, is there a way to do this with a single bash script? I've heard about SED but the documentation is confusing, could ...
2
votes
5answers
145 views

Remove multiple lines after a pattern match but only after pattern match

I am trying to remove all instances of a pattern match from a file if it matches a pattern. If there is a match, the (complete) line with the matching pattern and the next line get removed. The next ...
14
votes
5answers
22k views

How can I use sed to replace a multi-line string?

I've noticed that, if I add \n to a patter for a substitute using sed, it does not match. Example: $ cat > alpha.txt This is a test Please do not be alarmed $ sed -i'.original' 's/a test\nPlease ...
2
votes
4answers
91 views

sed load substitute from file

I'm attempting to replace a pattern in a file with the complete contents of another file using sed. Currently I am using: sed "s/PATTERN/`cat replacement.txt`/g" "openfile.txt" > "output.txt" ...
13
votes
6answers
17k views

SED: insert something to the last line?

The command inserts a tag to the beginning of a file: sed -i "1s/^/<?php /" file How can I insert something to the end of each file with SED?
1
vote
3answers
30 views

Concatenate Sequences under a Heading

I have a file like File1. With a heading for example ">Contig1" and I want to concatenate all the sequences under the respective heading. File1: >Contig1 ACCCCATACATCCAAA NNNNNNNNNNCATACA ...
1
vote
1answer
119 views

Pattern replacement

Please can anyone explain to me in brief the syntax of this command. Explanation in terms of how actually this command is written significance of each and every character used. I know the output of ...
0
votes
7answers
1k views

Unix sed command to find multiple string patterns

Scenario here is I need to replace "$$file_name=" value.. I have some 150 file names in a file as mentioned below.. Eg: $$file_name=abc.bbb.ccc to_be ---> $$file_name=abc.bbb.cccV1 AS_IS : ...
3
votes
1answer
45 views

Using sed to replace only the first two regex catches with different strings

So I'm trying to change two instances of a regex pattern to two different strings with sed like: sed -E "s/\\t([A-Za-z]|[-])+$/\\t`get_addr.py -1`/" /path/to/file.ext sed -E ...
8
votes
7answers
361 views

How can I “merge” patterns in a single line?

I am doing grep and sed and I get 2 lines of a file that I am interested in. How can I get these lines in a single line ending with the new line character? Now I am getting: pattern1 pattern2 ...
3
votes
3answers
48 views

Removal of lines with more than 'N' fields?

I am working on mac with sed, perl, awk, bash.. I have a large-ish (10GB) text file which has 13 fields (columns) of TAB delimited data. Unfortunately some of these lines have extraneous TABs , so I ...