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

learn more… | top users | synonyms

2
votes
2answers
211 views

Solaris using sequence generator at the beginning of each new line

I am new to shell scripting. I created a script that extracts data from a database and loads it to a csv file. I am using SunOS hstz1454 5.10 system. I am trying to do a sequence at the beginning of ...
4
votes
2answers
137 views

concatenate n lines with sed

Recently, I asked a question how to sort pairs of lines, and one of the answers suggested concatenating lines with sed, like this: cat file.txt | sed -n 'N;s/\n//;p' | sort -t";" -k43,43n | perl ...
0
votes
1answer
30 views

In bash I'm unable to concatenate a string using a variable defined from output of echo + sed

I'm trying to concatenate a string with $result. Placing another string BEFORE $result results in the two strings not being concatenated: ...
3
votes
1answer
204 views

Using SED to cut after 7th character of each line

Just trying to cut any characters after the 7th on each line of a text. I initially tried this: sed 's/(.{7}).*/\1/' TestText.txt But got this response: sed: 1: "s/(.{7}).*/\1/": \1 not defined ...
0
votes
4answers
71 views

Why isnt this simple grep/sed script working [closed]

I have a variable with, among others, a line that looks like this iBMC=277 I'm trying to match that like this: BMC="$(echo "$allnow" | grep iBMC=[0-9] | sed 's/iBMC=//g')" This makes BMC empty. ...
3
votes
2answers
58 views

Two sed commands in one command

I have a file with a lots of names etc. and with spaces in between. Now, I want to delete all trailing spaces and all empty lines in this file with sed. I have two commands for this task but I would ...
0
votes
1answer
24 views

write variable into specific line with sed command

echo -n "##### STATIONS? #######" read station text="stab_site " I need to write echo $text$station into the 27th line of text file. I used below command but it didn't work, sed -i ...
1
vote
1answer
21 views

Use both a variable and newline in BSD sed?

I can do this for example sed 's/\>\</\>\ </g' To replace >< with > < However, say I want a variable, like this: sed 's/\>\</\>\ <$1/g' It will interpret ...
1
vote
1answer
55 views

Why doesn't this work when I add it to the current sed being run?

I have a text file that looks like this: (empty) str int int If I add this s/^/\</g to my current sed (making it 's/\&/\</g;s/\</\ /g;s/^/\</g') this is the output. < ##only ...
1
vote
2answers
52 views

Sed - How can I insert a string after a certain line AND a certain character?

I need to insert some lines of code into a file. The problem is, I want to insert it after some function calling AND after the semicolon behind it, too. I'd like to achieve this just with sed, but I ...
3
votes
3answers
78 views

Print all matching lines and the previous unindented line

For example, lets say I want to find all lines with 'matching' and the previous unindented lines in this text. Container 1 some text some text matching text some text Container 2 some ...
0
votes
0answers
34 views

copying a set of lines into new file

I have a text file with 100,000 lines or more. I want to copy specific lines from the text file into a new file using: sed -n '1,1000p;1000q' orig-data-file > new-file However, I want to create ...
0
votes
1answer
32 views

sed: multiple ranges between patterns, with one pass

I have a script that reads a file of about 3GB and sends it through a pipeline involving a very small number of replacements in a very small part of the file. To accomplish this with minimal overhead, ...
0
votes
1answer
36 views

How to replace word with unique prefix and suffix?

testfile starts with: abc: aprefixAnYthingasuffix This sed operation is NOT working: sed -i 's/aprefix*asuffix/mynewword/g' testfile testfile ends with: abc: mynewword ...
0
votes
0answers
39 views

How to write into local file and set its content to a variable within a ssh command substitution

I would like to write on local file (or local variable if better) and set its content to a variable within a ssh command substitution. Here is my code : REMOTE_OUTPUT=$(ssh remote@server /bin/bash ...
2
votes
2answers
42 views

How to extract first part of filenames with same suffix?

I want to get at the prefix of a variable-length filename. In: some_filename_123-uniq.tar.gz Out: some_filename_123 In: some_filename_1-uniq.tar.gz Out: some_filename_1 This does the exact opposite ...
1
vote
1answer
21 views

How do I keep a part of the pattern matched and use it to replace on BSD sed?

I have found a soultion for GNU sed, and I also found something like \1, but the Terminal tells me it's "not defined in RE". But what I want to do is this: If I have a string that looks like this ...
0
votes
2answers
68 views

Search for a specific word in each line and print rest of the line

I have text file with server URL lines like: request get https://abc.net/search?q=hello/world/hello/word/search=5&size=10 request get https://abc.net/search?q=hello/world/hello/world/hello/word=5 ...
1
vote
3answers
57 views

How to sort blocks of lines with a specific line from the block?

I have a file with the data like this BEGIN hello2 5 world1 END BEGIN hello4 2 world5 END BEGIN hello6 4 END I want to sort the lines in the following manner based on the number inside the block. ...
2
votes
1answer
48 views

Add text between multiple lines with sed if previous line does not match pattern

I'm struggling to add <dl> tags around a definition list with sed, and perhaps there is an easier way to do it (I'd love to know). I would like to search a file for any lines that contain a ...
5
votes
2answers
62 views

sed string with bad chars

I have a random string with ugly chars in it: ÓˇÌ„ˇ™ÌÓ‹ÍÙ› ‹„ı That chars have to be eliminated. The Whitelist consist of: a-zA-Z0-9 -_* + ß ä ü ö () % @ € & = . and Space My first Script ...
1
vote
1answer
41 views

Add variable to beginning of line, not using sed

I have a text file where I'd like to add a string in the beginning of the line, however, it has special characters and sed complains about that. The warning I get is: sed: 1: "s/^/curl curliian.bxss ...
2
votes
2answers
164 views

How works sed 's/ /\ /g'

I found this line script in package added by Composer dir=$(echo $dir | sed 's/ /\ /g') I tried in the Git Bash $ echo $(echo "foo\bar\ foo/baz/ qux\\bax\\ " | sed 's/ /\ /g') foo\bar\ foo/baz/ ...
1
vote
5answers
109 views

print everything after a slash

I found how to print everything before a slash, however I need to print everything after a slash. I have a string like blablabal/important and I need to print only important. How should I modify the ...
0
votes
2answers
73 views

Command line tool to extract lines from a text file [duplicate]

I have a large file (say reads.fasta) which has around 5,000,000 lines and have another file reads_of_interest which contains a list of the line numbers of reads.fasta that I want to extract. Is ...
0
votes
1answer
47 views

Sort multiple csv files based on two columns within a directory

I have multiple .csv files in a directory called mydirectory. I want to sort all these files using some bash/awk/sed command first based on LeftChr column and then RightChr column and get the result. ...
1
vote
2answers
55 views

Invalid command code with sed

This is the command I am using: system_profiler | sed -n -e '/SATA/SATA Express:/,/Software:/ p' It gives me this error sed: 1: "/SATA/SATA Express:/,/S ...": invalid command code S I don't see ...
2
votes
2answers
51 views

How to replace values in a string using sed but keep the string intact

How to replace values in a string using sed but keep the string intact? For example echo "01XX1234" I'm looking to replace XX with a hex value and leave the 01 and the 1234 intact. I've tried every ...
0
votes
1answer
44 views

Find regex for timestamp in filename not working

I want find mail files in user maildir, without mailfilter, dovecot-index etc. All mail files have unix timestamp in its filenames, I think it is the easiest way. I used this regex: .*\/\d{10}\..*. ...
3
votes
2answers
54 views

How to remove certain characters (but not all) before one or more character(s)?

If I wanted to remove everything before a character (say "("), I would just do something like this 's/.*(//g'. Now I want to remove only certain things before a character/some characters, in my case - ...
7
votes
5answers
608 views

How to abbreviate /path/to/file to /p/t/file

I'm looking for an elegant one-liner (eg, awk) that will shorten a string of a Unix path using the first character of each parent/intermediate level, but the full basename. Easier to show by ...
3
votes
2answers
32 views

Search in specific column for pattern and output entire line

I'm working in HDFS and am trying to get the entire line where the 4th column starts with the number 5: 100|20151010|K|5001 695|20151010|K|1010 309|20151010|R|5005 410|20151010|K|5001 ...
1
vote
2answers
37 views

advanced sed print + replace [duplicate]

I am interested if there's a more elegant solution to the one posted below. []$ find ./abc -type f -exec sed -n 's/test/best/pg' {} + ; find ./abc -type f -exec sed -i 's/test/best/g' {} + The ...
1
vote
3answers
93 views

How to adjust the spacing between characters following a specific string?

I have a large file ~/foo.txt. Thousands of lines of the form SIZE : X Y are scattered throughout the file where X and Y are integers. My problem is that sometimes the integers X and Y are ...
2
votes
3answers
87 views

Can sed `y` commands be applied only to matching text (instead of to the entire line)

After writing this answer, I did some googling to find out if I could make the sed y// command be applied only to the match and not to the entire line. I wasn't able to find anything relevant. $ ...
1
vote
3answers
57 views

Sed entire word with when replacing single letters, with BSD sed?

I think I found some GNU solutions but I need BSD sed solutions. What I want to do is replace the entire word at once. If the word is "clayii", and my code is sed ...
4
votes
1answer
90 views

Script that Outputs the Average of Multiple Group lines, from a File

This is a "Small" portion of the file car_sales.txt that I am trying to extract information from (the full file has about 700 lines, with more vehicle brands than are listed here): ...
1
vote
1answer
49 views

“extra characters after command” in the sed command a

#!/bin/bash SVG=$1 CSV=$2 for p in "$CSV"; do LINEA=$(cut -d',' -f1 $p | sed '1d') LINEB=$(cut -d',' -f2 $p | sed '1d') if grep -iq "$LINEA" "$SVG"; then sed '/g id=\"..\"/a ...
1
vote
2answers
38 views

sed command to append some lines

I want append some lines through a bash script with sed. The script works but the output is wrong. LINEB="word" sed '/g id=\"..\"/a style=\"fill:$LINEB;\"' file.txt > foo.txt I get <g id="ai" ...
2
votes
1answer
49 views

Find and replace using parts of the found string in the replace string

Let foo be any string. I have to search a file for all occurrences of \LeftLabel{foo} and replace each occurrence with \LeftLabel{\textsf{foo}} but only if foo does not start with a $ ...
2
votes
3answers
135 views

Arithmetic on variable containing ip address

I've got variable network="198.168.1.x" # for ex. 198.168.1.254 I want to create new variables: next_serv="198.168.1.(x-1)" # for ex. 198.168.1.253 max_host="198.168.1.(x-2)" # for ex. ...
2
votes
1answer
38 views

Grepping and sedding IP from ip addr show

I'm trying to get just the local IP of my Arch VM. I've managed to get just the line containing what I want with grep, but I also want to trim it down with sed. inet <<192.168.0.16>>/24 ...
2
votes
1answer
49 views

Explanation for 'sed'

I have a huge text file called helo.lst in which every line starts with the format: #=GS N4TFW2_FUSC1/96-161 AC N4TFW21 I'm only interested in the N4TFW2_FUSC1 part. I need to extract this ...
1
vote
1answer
24 views

Displaying a file in another file [duplicate]

I was wondering how to print the contents of a file above the occurrence of a pattern in another file. I know that using sed -n '/^PATTERN/i\\ INSERTING' filename would insert above the pattern but ...
1
vote
2answers
91 views

How to display each day of a month on a separate line using tr, sed and cal

How to use commands, cal, sed and tr and the pipe to display the numerical days (e.g., 1, 2, 3, etc.) of a month, with each day on a separate line I've tried: cal 12 2015| sed '3,7p' | tr -cs ...
0
votes
1answer
24 views

Display only records if the value in the last column ends with a 4

Display only those records in a file if the value in the last column ends with a 4. How do i do this?
0
votes
1answer
31 views

Filter log on final letter in string [closed]

I have a below requirement to filter a word. My logs will be reporting with codes TML2345I,TML8383E & TML9929W. I want to filter the errors only with strings which have the last character as E. ...
0
votes
1answer
17 views

Print indentations with whitespace only in tree command

I want to list the subdirectories of a directory using tree command. But I don't want to print indentation lines. I only want to have the whitespaces instead. I couldn't find the correct parameter ...
2
votes
2answers
118 views

Curl url txt file, but grep each url seperately from single file

Ok, here's one for you guys that is puzzling me. So I have a text file with lots of url's in it. I'm using curl -K "$urls" > $output to spit the output to my output file. Now for the output of ...
2
votes
2answers
82 views

Insert text in specific lines of a file? with special characters

i know this has been answered several times, but i can't figure out how to use it for my project. i have several php files and want to add a new string between line 11 and 12. this is the string i ...