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

learn more… | top users | synonyms

11
votes
3answers
563 views

Merging two files, one column at a time

I have 2 large files (3000 columns, 15000 rows) of the following format file1 (tab-separated): 1/0 0/0 0/0 0/0 1/1 0/0 1/1 0/1 0/0 file2 (tab-separated): 3 5 2 1 7 10 3 4 3 I'd like to combine ...
0
votes
3answers
51 views

How to delete lines in file from specific position to the end of the file

I have a file with many lines. I want to delete from the line 1458 to the end of the file. I think that I can do it with sed, but I do not know how to.
3
votes
2answers
26 views

Extract parts of csv filename in columns

I have a set of .csv files in a directory, say abc_q1_w1.csv defg_q11_w2.csv hijk_q11_w3.csv I want to write a script that extracts parts of the filename and put them in columns, i.e abc q1 ...
-1
votes
4answers
85 views

Extracting IPs from a web list

I am trying to extract IPs from a huge web generated list which contains a mix of text, IPv4 & IPv6 IPs, newlines, IP ranges etc. Below is a snippet from the list: ; Spamhaus DROP List 2016/07/...
2
votes
2answers
121 views

Text manipulation: Extract everything inside brackets

I have a text file where every line is in a similar format to this: %#&#%# [Dinero] / Money / !#@%$@ [Dia] / Day / $%&$^#@ [Perro] / Dog / I am looking to extract the words ...
2
votes
2answers
44 views

sed to print pattern that spans across lines

I'm learning sed and I'm currently stuck with the following problem: I want to extract the lines found between ^C's from the following output: banner exec ^C This is the banner ^C banner motd ^C ...
3
votes
2answers
124 views

sed to split verilog bus into individual port

I want to convert a particular Verilog Bus into individual split form using sed or awk command. Input module test ( temp_bus[3:0], temp_B[1:0] ) input [3:0] temp_bus; output [1:0] temp_B; ...
1
vote
1answer
42 views

Is there a way to use apostrophes in sed commands? [duplicate]

Is there a way to use apostrophes ' with the sed command? I mean this: - sed '-2 a\\ What's up?' text I am talking about the apostrophe in "What's up". If I do it like this it gives me an ...
-1
votes
0answers
64 views

sed replace or add variables into file

I'd like to use the following function (which is working for me): if grep -q HOSTNAME=noob hostname.ini; then sed -i "s/HOSTNAME=noob/HOSTNAME=pro/g" hostname.ini; else echo "...
1
vote
1answer
43 views

bash for loop variable not changing

I am iterating over indexes which are used to create data file id's. When I create the file name (data_file) and echo it, it prints properly, but when I try to sed that variable into the desired files ...
0
votes
3answers
47 views

Pad number within string and rename file [duplicate]

I have some filenames as such. a2.txt.gz d12.txt.gz k5.txt.gz m4.txt.gz ... one character followed by 1 or 2 numbers followed by .txt.gz. I would like to add single numbers with a zero before the ...
1
vote
1answer
92 views

Remove commented lines except one comment using sed

Not very familiar with complex usages of sed, but I have this requirement now. I have a file 1.txt with contents in the following pattern: sree@amby01:/home/sree : cat 1.txt SSL this first line #...
4
votes
4answers
355 views

How to print lines between pattern1 and 2nd match of pattern2?

Test file is given below: PATTERN1 a b c PATTERN2 d e f PATTERN2 g h I want to print line between PATTERN1 and 2nd match of PATTERN2: PATTERN1 a b c PATTERN2 d e f PATTERN2
0
votes
1answer
47 views

How can I remove duplicate entries in linux log

I've turned mysql general log on and I've been logging every transaction that occurs for the last week. I also have a cronjob running every 5 minutes doing some mysql transactions which is polluting ...
2
votes
1answer
51 views

sed unterminated `s' command [closed]

I'm getting: sed: -e expression #1, char 46: unterminated `s' command On this code: sed -i.bak -c 's|net.ipv4.ip_forward.*|net.ipv4.ip_forward = 1|' /etc/sysctl.conf My editor is showing that ...
1
vote
5answers
69 views

Getting substrings in between delimiters multiple times from one string without looping

I have looked all over for a solution to this, but I couldn't find one. I am using qstat -x to pass a large string of job information. The output from qstat -x is in XML format. The substrings I am ...
3
votes
2answers
123 views

Add a line below each line matching a pattern only if not already present

Can sed add a new line below a specific content, if the input content exist then leave it ? Current content of the file ssss Hostname example.com Os version rhel5.6 apache 4.2 Hostname example2.com ...
2
votes
2answers
72 views

Retrieving five random items from a list using sed

How can I retrieve randomly selected five different items from a list using sed? My list is something like below. Each item begins with an asterisk at the beginning of a line. * asterisk* star ...
0
votes
1answer
44 views

when i used sed -n ,why this err about sed -n ' $j,4800p' [duplicate]

LINEnum=$(grep -nr "#32" IM_DlCtrlRef.txt | cut -d : -f 1) for j in $LINEnum do echo $j sed -n '$j,4800p' IM_DlCtrlRef.txt >> IM_DlCtrlRef_bak for insertl in {1..4} do ...
2
votes
2answers
29 views

less combined with sed color sequences prevents page breaks

If I use the command below which colors the lines output in yellow, it essentially turns less into cat, printing out the entire file and disabling all of the features I need with less, such as ...
1
vote
3answers
51 views

Reformatting negative numbers with sed or awk

I have a text file that contains some negative numbers except, they are formatted as: 00000012- With the hyphen after the number. I am processing the file using awk to reformat it into SQL insert ...
0
votes
1answer
56 views

Using multiple sed commands for text manipulation

I have a bash file, in which I would like to make several changes. sed -i 's/ / /g' testfile sed -i -e 's/#[a-z]$//' -e '/^$/d' testfile sed -i 's/ *$//' testfile sed '/##=/ r header' ...
2
votes
2answers
116 views

sed - find correct color sequence for ANSI extended 256 colors

I am currently using the following command to use the standard ANSI colors with the tail command: tail -f syslogfile | sed -e 's/\(.*MAJOR.*\)/\o033[93m\1\o033[39m/' This is bright yellow however I ...
0
votes
2answers
52 views

How to add a column of numbers to a array data file

I have a data file with two columns of numbers: input file 4.182 4.1843 4.184 4.2648 4.2281 4.0819 4.2204 4.1676 4.0482 4.1683 4.0156 4.2895 4.4504 5.2369 4.3776 4.4979 4.3797 4.1372 4....
0
votes
1answer
28 views

How to multiply two columns in a file by a constant number

I have a file like this. input data 4.2394 4.4569 4.2427 4.1011 4.2879 4.1237 4.2106 4.4844 4.2373 4.1071 4.1322 4.0502 4.3103 4.4255 4.4342 4.5262 I need to multiply each element by a constant ...
3
votes
3answers
42 views

awk to cut portion of a field and still print entire line

I have this line: 08:30:02.480507 IP 192.168.100.150.65119 > 192.168.100.151:53 59865 [1au] A? click.em.redbox.com. (48) I need to cut out the ".480507" in the timestamp so the line looks like ...
1
vote
3answers
88 views

Find and Replace with command line

my log.txt file includes ... mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 ... how can I replace mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 with mynetworks = 127.0....
1
vote
1answer
32 views

How to unwrap paragraphs using sed

I have a text file that contains paragraphs wrapped at 76 characters with an empty line between paragraphs. I'd like to use sed to convert this to a file that has one line per paragraph, no blank ...
2
votes
2answers
110 views

Extracting positive/negative floating-point numbers from a string

I am trying to extract numbers out of some text. Currently I am using the following: echo "2.5 test. test -50.8" | tr '\n' ' ' | sed -e 's/[^0-9.]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' This ...
-2
votes
1answer
55 views

filtering pattern in a file using sed or grep

I'm new to Linux. I'd like to store all the unique usernames present in the extended.log file into a new file using the commands awk, grep, and/or sed. The following are the field-names in my file, ...
0
votes
0answers
30 views

shell scripting using sed

I want to compare specific string surrounded by alphanumeric and some special chars in file. For example: 5efa4083-7157-437a-ab9a-652ced2fc2ea;: AAA :;5efa4083-7157-437a-ab9a-652ced2fc2ea;BBB I ...
0
votes
3answers
50 views

Find a text in a file and Insert a text after it

I have a text file. The text file contains the below content. ServerName ServerAdmin webmaster@localhost DocumentRoot I need to insert a text www.mydomain.com, after the ServerName. Is there any ...
0
votes
1answer
86 views

Explanation of sed in shell script

I found a snippet of code online that would actually help with creating the frequency of each word in a text file however I would like someone to explain exactly how it is doing so Especially the ...
0
votes
2answers
35 views

Why you cannot have Tab in sed -i “1i \t 'word'” file?

Pseudocode printf "Masi \nwas \nhere" > /tmp/2 sed -i "1i \tword \t 'include word'" /tmp/2 Expected output: tab before the first word. Output: tword 'include word'. Trying to use variable to ...
1
vote
1answer
451 views

How to sed -i '`[echo $var]`i word' file?

Code: pos=2; printf "Masi \nwas \nhere" > /tmp/1 sed -i '`echo $pos`i huhu' /tmp/1 Expected output: Masi huhu was here Current output: sed: -e expression #1, char 1: unknown command: ``'
0
votes
1answer
45 views

Why Blank output of this `sed … <<END_SED | cut …`?

The following sed returns (...| od -c) without ...|cut -d "\n" -f 0000000 1 9 2 \n 2 5 0 \n 0000010 Pseudocode # http://unix.stackexchange.com/a/290407/16920 sed -n -f - /usr/share/...
2
votes
1answer
417 views

Read from and append to file at the same time while preserving end-of-line

Conceptually I have an easy task... I have [loosely] structured data in a file: Testing: debug, default CXXFLAGS <100's of additional output lines> Testing: release, default CXXFLAGS <100's ...
3
votes
2answers
368 views

grep: How to find Closing Bracket?

One part of /usr/share/X11/xkb/symbols/us starts with xkb_symbols "dvorak" { and ends with the closing curly bracket }; which line-number I want to find. partial alphanumeric_keys xkb_symbols "...
2
votes
1answer
44 views

How can i replace multi line string using sed? [closed]

I have tried the following command:- [root@10 bin]# cat temp asdjahskjdhajksdhjkashjahfksjdhfkjsddhflkdsjhfklsdajhfklajshdkjalsdlkjasdhkljas hskdajkdhkjas cast $recv $UE_CAPABILITY_ENQUIRY ...
-9
votes
2answers
50 views

how to print in a single line with pattern matched

student:xxxxx student:yyyyy student:zzzzz student:kkkkk student:. teacher:aaaaa teacher:bbbbb teacher:lllll teacher:. student:sssss student:mmmmm student:. output should be, student:xxxxx yyyyy ...
1
vote
2answers
30 views

Insert text inside stanza at the end of the data string

I have a file I need to echo text into at the end of the file from the command line, the problem is the data is in a stanza, all within curly braces. This is the end of the file: text-string1,text-...
1
vote
2answers
46 views

sed: Grabbing and replacing all data between last backslash and a comma

I have data in a csv format that looks like this: F1309-042543,07/14/09 01:39:25,N/A,C:\windows\system32\netsh.exe,N/A,True F1309-042543,11/21/10 03:24:02,N/A,C:\Windows\System32\networkexplorer.dll,...
1
vote
2answers
54 views

Specifying comma-separated command line parameters based on text file lines

I want to run a command like: command argument --parameter=param1,param2,param3 where param1, param2, param3 may be any number of parameters coming from a file params.txt, each on one line. I can ...
3
votes
3answers
141 views

edit repo file with sed

I want to edit manually my repo file from the command-line, preferably using sed. How can I do that based on the repo-name I want to edit? I want to search for a specific repo-name (example: reponame-...
0
votes
3answers
35 views

Escaping regex sed (1 lowercase, 1 uppercase, 1 symbol, 1 number) for bash

The goal is to remove every line not containing one uppercase, one lowercase, one special character and one number from a file. I am unable to run this regex: (?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=....
0
votes
2answers
43 views

How to print lines using find and sed in multiple files?

I've many files in a directories structure. I want to extract some string (namely urls) by using a regexp, from those files. I've tried this : find . -path "*alder/ * / * .html" -print | xargs sed -...
2
votes
2answers
76 views

How to get the last occurrence of lines between two patterns from a file?

I have a log file which reports on the output of a process, I'd like to extract all lines from between the last occurrence of two patterns. The patterns will be along the lines of; Summary process ...
3
votes
3answers
246 views

Use sed with back references

I am trying to remove a space between 2 strings, they are like this: 312.2 MB 123.34 KB 487.1 GB I want to change them to: 312.2MB 123.34KB 487.1GB I've been trying and I can get: echo "312.2 ...
0
votes
1answer
44 views

help to analyse a log file

How does sed 's/\[//' work in case of analyzing log files for the time stamp fields? I found a solution online and I saw this line "$(head-1 "$1" | awk '{print $4}'| sed 's/\[//')". Can someone ...
-5
votes
3answers
110 views

How to add all the numbers of a specific column in UNIX without using awk command?

Let us take a sample file with the contents below, an expense report containing items and their prices: $ cat file Item1,200 Item2,500 Item345,900 Item20896,800 Item111,600 Now, a friend challenged ...