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

learn more… | top users | synonyms

1
vote
1answer
98 views

How can I run sed to replace lines in a file or do similar things in PHP?

I am writing a web frontend for configuring OpenWRT. I'm using PHP to execute commands and all the uci commands are executing without issues. However, OpenVPN configurations cannot be done via UCI and ...
3
votes
1answer
36 views

Recursive find/replace, taking directory depth into consideration

I've had good luck doing basic find/replace: find . -name "*.php" -exec sed -i -e "s/<?php include 'http:\/\/www/<?php include '..\//g" \{\} \; I am wondering if there is a way to achieve a ...
0
votes
3answers
82 views

How to substitute a string in file with another file?

I have several files that all contain a string. This string needs to be replaced by the whole content of another file (that can possibly be multi-line). How can I do this? What I need is something ...
2
votes
2answers
65 views

Sort a list and separate items with commas instead of newlines

I want to list all the filesystems in a single comma-delimited list, in dictionary order. I'm using this command: cat /proc/filesystems | sed 's/nodev//' | sed 's/,$//' My output looks like this: ...
0
votes
1answer
79 views

Why does sed replace all occurrences instead of only the first occurrence? [duplicate]

I'm trying to replace the first occurrence in a file using sed: sed -i s/he/He/ dummy.txt Instead of replacing first occurrence, it replaces all the occurrences, even without /g. According to the ...
2
votes
2answers
69 views

Are there any sed scripts built in to mainstream Linux distros?

When I was studying bash, I found it very helpful to go and study the bash scripts already present on a clean install of Linux—/etc/profile, for instance, and anything in /etc/rc.d/init.d/. These are ...
7
votes
3answers
74 views

Remove certain characters from multiple files with whitepsaces

I'm trying to rename a bunch of music tracks in a directory, but I got this error: When moving multiple files, last argument must be a directory This is the script: for file in * ; do mv ...
0
votes
1answer
47 views

Remove trailing spaces from first field only

I have a multi-lined, tab-delimited file like this: 99999bbbbbt999999bbt99t99t99t99 (where b=spaces, t=tabs) Trailing spaces in the 2nd field are ok, but the trailing spaces in the 1st field need ...
0
votes
2answers
41 views

Merge two lines of a file and adding number in the end of lines

I have a large text file abc.txt with the following format: BALT -6.110 130.600 4.06874414 -0.03254425 BALT -6.620 154.460 3.92286595 -0.17842244 BARM 32.740 140.680 3.94326190 -0.15802649 I need ...
4
votes
3answers
51 views

Inserting a text between 2 patterns

I have a .xml file which contains the below <controller-host></controller-host> <controller-port></controller-port> <tier-name></tier-name> ...
1
vote
2answers
42 views

Extract keyword from line

I need to extract similar patterns from data below. The pattern starts with '.' or '' then to the keyword I want, then followed by ''. We can assume the format is LSUFE-numbers_ PCRF-numbers_ ...
2
votes
3answers
340 views

Shellscript to remove particular tags in HTML file

Can anyone please guide me with writing Shell Script command for following scenario. I have an html file with all tags in a single line like this, ...
38
votes
7answers
85k views

Delete First line of a file

How can I delete the first line of a file and keep the changes? I tried this but it erases the whole content of the file. $sed 1d file.txt > file.txt
0
votes
2answers
368 views

Shell Script to remove double quotes within the column value

I have a input text file with 10 columns, while processing this file, in one of the middle column, I am getting this type of data.I required column value to be as below: Input Column value: "This is ...
1
vote
2answers
241 views

How can I print the second to last line of many files into one file? [duplicate]

I have many CSV files in one directory which have various lengths. I'd like to put the second to last line of each file into one file. I tried something like tail -2 * | head -1 > file.txt, then ...
0
votes
3answers
43 views

sed - How do I match this pattern?

I'm fairly new to using sed and I am having trouble finding out how to remove some of the first line of text from each block. Here is my original data: "s220823vaps2512": { "hostname": ...
-1
votes
1answer
82 views

how to extract lines between 2 patterns, and save them to different files

Suppose we have this file: rrows 35 cols 51 spin 2 %band5 efermi=0.012538 0.004352 0.004317 0.004213 0.004047 0.003828 0.003570 0.003291 0.003011 0.002754 0.002544 0.002408 0.002376 ...
1
vote
1answer
54 views

Merge the duplicated lines and add “N/A” in the end [closed]

I have the following lines in a text file. I want to merge the duplicated lines and add "N/A" as needed at the end of the line, to have six columns on each line: 302C21;tSMe S123C;1.17 ...
1
vote
2answers
68 views

find a field and move it to before the end of the line

I have the following line and I want to find the field containing "ABCD" and move it just before the last field on the line. string1;string2;xxxABCDxxx;string3;string4;string5;string6 Output ...
2
votes
3answers
99 views

Merge duplicated lines which have the same first three fields

I have the below input (a thousand lines). I want a sed command to merge duplicated lines with the same first three fields which adds only different fields or deletes "N/A": D04005;4;279;0;0;SSM-4-1 ...
0
votes
1answer
116 views

sed - find and replace text containing “/”

How can I search and replace text that contains / with sed? I currently use the following command which doesn't work sed -i "s/queue_directory = /var/spool/postfix-secondary/queue_directory = ...
0
votes
1answer
189 views

load bash comands from file one per line and execute them for each file in a directory

I'm trying to write a bash queue which will be able to load command sets from a file and then execute each over a set of files. The prepared file should serve as a template for execution in manual ...
4
votes
3answers
952 views

How to grep lines between start and end pattern? [duplicate]

I have a file which is having following content: zdk aaa b12 cdn dke kdn Input1: aaa and cdn Output 1: aaa b12 cdn Input 2: zdk and dke Output 2: zdk aaa b12 cdn dke I could use below ...
1
vote
2answers
127 views

How can I remove characters of text in one line before a specific character?

How can I remove characters of text in one line before a specific character? I want to remove characters to the left of the first letter "F" Example: null###F01|54646466|00K4234001|IFD|1990101 ...
6
votes
1answer
3k views

How to grep all lines where starting line ends with a pattern and ending line ends with another?

I want to start grepping from the lines that has CK at the end of line and stop grepping when the line has D at the end. I tried grep "$CK" "$D" file..txt, but it didn't work. Input: kkkkkkkkkkk ...
0
votes
1answer
42 views

\1 - what does this sed action mean?

I am using this sed command to extract a json value for a key, say MyKey.. sedPattern='s/^.*"myKey":"\([^"]*\)".*$/\1/' For a json string like, {"myKey" : 1, "yourKey": 2} It returns what i ...
-1
votes
2answers
57 views

Find file, Edit text within files [closed]

I am trying to find a file and then edit the text within these files. This is what I have so far, but sadly I'm not that well versed. Help please? grep -rl "wrongtext" ~/Library/Calendars ...
1
vote
1answer
80 views

How do I insert a character at the beginning of each line of a paragraph that contains a string

Sort of different request here: I have a text file in which I need to place a @ character at the beginning of each paragraph (paragraphs=separated by blank lines) that contains a certain search ...
2
votes
1answer
68 views

Using sed to replace n characters before a keyword including the keyword?

For replacing everything before a keyword piece of code below works well. sed 's/.*keyword/string-to-be-replaced/' But for replacing n characters before the keyword including the keyword I couldn't ...
0
votes
2answers
104 views

Prepend lines with file prefix using sed or awk

How do I change a sample input file so as to get something like the output file below? Input file: /tmp/logs 10/13/15 00:19:13 templogs1 10/13/15 00:19:13 dummylogs 10/13/15 01:19:12 test 10/13/15 ...
1
vote
1answer
79 views

Is sed run as a different user?

I have a sed script that changes some content in /etc/shadow. The actual change isn't important, I will put it just as an example: root@device:~ sed -i 's/root:\(.*\):0:0/root:\1:10:0/' /etc/shadow ...
2
votes
1answer
73 views

How to combine POSIX character classes of a regex?

I need to combine different character classes in a regular expresion used by sed. I need to match [:word:] and the minus symbol -. How does that look like? All my attempts failed at trying or ...
2
votes
3answers
227 views

remove lines that contain a specific text in a file

I have a file with a list of emails in it and each line has an email in it. I want to remove lines that contain the string example.com or test.com. I tried this: sed -i 's/example\.com//ig' ...
0
votes
2answers
74 views

sed command to comment out specific block of code containing search string [closed]

I have a listener.ora file for Oracle in which I need to comment out a block of lines specified by a search string, e.g., >(DESCRIPTION= >>(ADDRESS_LIST= ...
1
vote
2answers
53 views

join two files based on column when there is no one-to-one correspondence in bash script (awk, grep , sed)

file1.txt 112|9305|/inst.exe 112|9305|/lkj.exe 112|9305|/dje.jar 112|9305|/ind.pdf 112|9306|/ma.exe 112|9306|/ngg.pdf 112|9307|/jhhh.dat 112|9312|/ee.dat 112|9312|/qwq.dll file2.txt ...
1
vote
1answer
38 views

stream editing tools: output what's scraped

I was just wondering if it was at all possible when using different stream editing and text processing/manipulation tools; such as grep, cut, sed, awk, perl etc. Are there ways to output (in the form ...
0
votes
0answers
22 views

In sed, what's the best way to apply blocks of commands to multiple addresses?

If I have a block of sed commands I want to apply to multiple addresses that aren't an address range, what is the best way to do this without repeating the block separately for each address? The only ...
1
vote
2answers
47 views

awk regex for apache rewriterule

I have these variant of urls RewriteRule ^/trendy/the-reason-for-example/? http://www.example.com/trendy/the-reason [NC, L, R=301] RewriteRule ^/lol/2015/10..._for-example ...
2
votes
4answers
150 views

Only leave longest line when first column is duplicate

INPUT: user@server:~/bar/foobar$ SAT=$(date -dsaturday +%Y-%m-%d); SUN=$(date -dsunday +%Y-%m-%d) user@server:~/bar/foobar$ awk 'BEGIN {FS="'^'"} {print $1"\t"$3"\t"$6}' STs.csv STt.csv | egrep -w ...
3
votes
2answers
146 views

bash command that outputs the result of a previous pipe

I've got a text file outputted from a WebSpider. The Spider extracts all sentences from a given list of URLs. What I need to do is then process this file and find all lines that contain more than 65 ...
6
votes
2answers
343 views

Delete multiple lines in a file

Usually I find my answers in the previously answered questions, but not this time (or I don't understand the answers and can't modify them to do what I want them to do), so I'm asking my first ...
0
votes
2answers
33 views

need to replace end of column with a character if it does not already exist

I am trying to use awk but can use anything else that will work lets say i have a file with these lines ewhueh wnlknlweknlnw/ qwejmdpdhhd 47578 enhkine enoekjohnkjiof elkjehbjuwekjuk 88989 ehnjkine ...
4
votes
3answers
145 views

Remove long string from files

Files on my server have been corrupted. I want to remove a 13000 character string from all PHP files containing it. The string looks like: ?php if(!isset($GLOBALS["\x61\156\x75\156\x61"])) { ...
0
votes
3answers
70 views

sed command not replacing a text with newline character

The Unix sed command doesn't recognize a backslash escaped n \n as a newline character. For instance, the command sed "s!,!\n!g" is replacing commas in the input text with the letter n and not a ...
0
votes
1answer
93 views

SED find and replace element in filename with incremental values

A sample filename is Sample-0.0.1-SNAPSHOT.jar (result of a maven build in jenkins). My goal is to run a shell script that executes on failure and what that script does it adds incremental numbers to ...
0
votes
1answer
39 views

Can you replace displayed text with something else in command window?

I am trying to manage user accounts. They are basically just numbers, but I would rather see user last names on the screen. Is it possible to automatically replace displayed text with something else? ...
5
votes
3answers
3k views

sed: multi-line replace of config block

I have some configuration files that basically look like (...content...) # BEGIN DYNAMIC BLOCK - DO NOT EDIT MANUALLY (... more content ...) # END DYNAMIC BLOCK (... even more content ...) Now, in ...
1
vote
1answer
53 views

sed - simple regular expression replacement not replacing

I'm trying to use sed to remove any subdomain prefix from a list of hostnames, but it's not replacing anything when I use a regular expression. Here's the simplified example that's not working: echo ...
2
votes
2answers
54 views

Extract content between tags

I have a file with data as below I want to extract the lines and write to another files. The o/p should be I want to do this in a unix shell script.Can someone hellp me on this. I know this can be ...
12
votes
6answers
2k views

How to delete the last column of a file in Linux

I want to delete the last column of a txt file, while I do not know what the column number is. How could I do this? Example: Input: 1223 1234 1323 ... 2222 123 1233 1234 1233 ... 3444 125 0000 5553 ...