Tagged Questions
3
votes
3answers
71 views
using sed to replace pattern with hash values
I want to search the file and replace specific pattern with its hash (SHA1) values.
For example, let file.txt has the following content:
one S56G one two three
four five V67X six
and I want to ...
0
votes
0answers
23 views
Variable Substitution in sed [duplicate]
I have this script and I need to count the lines inside a file, then pass the number of lines counted to a for loop so that I can get the contents of each line and put it in a variable.
My question ...
4
votes
5answers
210 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
Thanks.
1
vote
1answer
51 views
How to assign result of sed to variable
Below is a complete copy of a demo I'm using to to figure out to get the sed command to get rid of the spaces in a persons name, and compress it down to not have spaces.
Once this is done, I want to ...
1
vote
2answers
63 views
Improve sed command to replace first instance of character and all following characters?
Input:
<e1 name="file1" id="id1" anotherId="id2">
Desired output:
file1
I can get what I need with this:
echo '<e1 name="file1" id="id1" anotherId="id2">' | sed ...
3
votes
1answer
60 views
How can I write a sed script to delete numbers from a line?
Sample File
a.b.gg3d 0000000001 0000000001 y
a.b.112a 0000000000 0000000001 y
a.b.gg3s 0000000001 0000000001 y
a.b.1111 0000000000 0000000001 m
I have a file that includes about three thousand ...
0
votes
6answers
238 views
Using cut/awk/sed with two different delimiters
I have the following cases:
[email protected]
[email protected]
[email protected]
I'm trying to convert these to
[email protected]
[email protected]
[email protected]
So it should remove everything from ...
2
votes
3answers
113 views
Using a reference to a bash string variable in sed
I'm a relative Linux novice. Suppose that I have a text file a.txt that contains the following text:
A
B
C
Now, if I want to change the text on line 2 (which contains B), I can use the following ...
2
votes
2answers
103 views
Passing a variable to sed
I cannot not use a shell variable in sed in the $NUMBER form. I have this line in my shell script:
cat shared.txt sed 's/whatever1/$2 ... whatever2/' > shared2.txt
The result in shared2.txt ...
5
votes
4answers
205 views
sed how to substitute when string has “http://” in it?
I have a file xx that has the following contents:
@base_url = "http://dmstaffing-stage.herokuapp.com/"
I want to use sed to eliminate this line (replace with nothing). I have used this sed ...
3
votes
3answers
105 views
checking data in columns when a data or some may be missing or present?
I am not sure if this possible.
say i have columns like :
Team Colour Game Rainfall PlayerName
XYZ Blue Cricket Yes Kapil
suppose i need to search ...
4
votes
3answers
405 views
Why is sed giving me an error about an unterminated `s'?
I have a set of sed replacements in a bash script and am getting an error about an unterminated `s' command. Here's what the sed line looks like:
sed -n -e "s/TMPFOO1/$FOO1/" -e "s/TMPFOO2/$FOO2/" ...
2
votes
2answers
165 views
add “#” in begining to selected lines in File
Requirement : ADD someting (i.e #) in begining of every line in file A which is grep from file B
File A
abcd
abdc
sdfg
asdfa
jon
ram
File B
jon
abcd
grep file B from file A and add "#" ...
3
votes
4answers
284 views
Replacing pattern after nth match is found on each line?
I have a file containing lines:
india;austria;japan;chile
china;US;nigeria;mexico;russia
I want to replace all the occurences of semicolon on each line with e.g. ;NEW;, but starting from the 2nd ...
1
vote
1answer
83 views
How to parse this output and put it into an array?
i have expect script whose output looks like :
I would like to extract all words like Gi/02/12 etc in an array or a list.
VLAN Name Status Ports
---- ...
-1
votes
2answers
636 views
Bash -Extract characters from string
Suppose I have the following variable
$string = 'cat somecommandhere'
Now the output of the above cat command is
test="Thanks serverfault"
I want to extract the value between the quotes and ...
2
votes
1answer
55 views
how do i suppress seds unnessary output while using option -e?
why n option is not working here
[root@testgfs2 final]# echo a b c | sed -ne 's/[[:blank:]]/"/gp' -ne 's/^/"/pg' -ne 's/$/"/pg'
a"b"c
"a"b"c
"a"b"c"
i just need :
"a"b"c"
actually ...
2
votes
2answers
126 views
changing pattern by removing spaces?
file :
hi hello,new york, , ,brazil site ,brazil
january,month is feb , , ,indiana jones
task:
delete all horizontal space (spaces and tab) between any two comas (like , ,) if ...
1
vote
2answers
210 views
reading from a file and changing its pattern into an array?
I have a CSV file containging entries like this :
ipaddress,VLAN,VLANid
10.192.168.1,vlan-xyz,3
10.192.168.1,vlan-abc,8
10.192.168.1,vlan-mnp,11
10.192.163.24,vlan-llz,3
10.192.163.24,vlan-bbz,5
...
1
vote
2answers
116 views
sed pattern matching with deletion over a line
I want to delete only some spaces(say 4) after the line$#*425 with space
I am not able to complete it please suggest
echo "line$#*425 with space " | sed 's/ /\1 \2/g'
i am thinking of ...
4
votes
4answers
2k views
Text between two tags
I wanna retrieve whatever is between these two tags – <tr> </tr> – from an html doc.
Now I don't have any specific html requirements that would warrant for an html parser. I just plain ...
0
votes
1answer
106 views
How to delete number of lines from file repetitively
I've read How do I delete the first n lines of an ascii file using shell commands?, it is helpful. However I've a file something as below (please consider 2 columns as 2 different files):
1 4
1 4
1 4
...
3
votes
5answers
442 views
Remove lines based on pattern by keeping first n lines
I need to remove lines from a text file based on pattern but I need to keep the first n lines of that pattern.
Input
% 1
% 2
% 3
% 4
% 5
text1
text2
text3
output
%1
%2
text1
text2
text3
I used ...
1
vote
3answers
357 views
linux/solaris + verify duplicate valid IP address from file
what the best way to find duplicate IP from file ( I have ksh script in this script I need to write function that check for duplicate IP )
for example if IP - 192.1926.23.52 exists twice in file - ...
3
votes
2answers
826 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
470 views
Linux + replace STRING/WORD in file according to rule
My target is to replace any word/string/Any characters in file
with new word/string according to the following rules:
If numeric characters on the left side of word/string then we not replace the ...
1
vote
3answers
241 views
Script matching literal pattern over multiple lines?
I have a multi-line string in the variable $PAT. $PAT must be searched for within the file $FILE. If $PAT is in $FILE, it needs to print the file with $PAT removed. If $PAT is not found, then print ...
1
vote
1answer
558 views
Diff, show modified line from right file only
I have file a and b and I would like to output lines of b that changed since it was cloned from a. Just the modified lines, no surrounding context, no diff offset marks.
How can I do that using shell ...
0
votes
2answers
179 views
Easiest way to find/replace in a file using a list?
I have a file, foo.txt, and a regexp I want to find in that file. Each time I find the regexp, I want to take a line from another file, bar.txt, and substitute it in for the regexp match I found in ...
2
votes
1answer
1k views
sed special character replace not working in shell script
I want to write a script 'test.sh' that will take a user's input and replace all special characters with a '\' + the character. My script:
#!/bin/bash
echo 'input='"$1"
arg=`echo "$1" | sed ...
3
votes
6answers
183 views
Extracting help message from script itself
I want that my help message be extracted from the script itself.
#!/bin/bash
#
# foo - do things
# Author: John Doe <jhon@doe>
# ----------------------------------------------
# SYNOPSIS
# ...
2
votes
1answer
369 views
Inserting a file into another file using sed
I want to insert the contents of file1 into file2 after a matching PATTERN. I want to do it only after the first occurrence of the PATTERN.
I would like to know the modification I need to make to ...
4
votes
1answer
588 views
How is this sed command working?
I want to understand, how following sed command is working:
sed -nre "/jar$/ { x; d; }; /class$/ { x; p; }
Actually it is part of another command to find a particular class file from the given list ...
1
vote
3answers
786 views
How to rename files using sed?
This is an sed expression that was created by a contributor here. More specifically by @craig-sanders.
$FileToMove is set to "2012-09-01 Home insurance quote_zz20.pdf"
# use sed to extract folder ...
8
votes
3answers
1k views
How to sort the string which combined with string + numeric using bash script?
This is the data what I want to sort. But sort treats the numeric to string, the data it no sorted as I expected.
/home/files/profile1
/home/files/profile10
/home/files/profile11
...
3
votes
6answers
1k views
Text file look-up by column
I have a file in this format:
[#] OWNER_NAME NAME SIZE
[6] Robottinosino Software 200
[42] Robottinosino Ideas worth zero 188
[12] ...
2
votes
2answers
180 views
How do I ad an if statement (regarding punctuation in a word) to this bash script
I have this bash script
#!/bin/bash
cat $@ | while read line
do
for word in $line
do
echo $word | circling-the-square
# here's where i need to add the if statement:
...
2
votes
1answer
188 views
Is it a good idea to supplement shell script with perl purely for use of regex?
I'm looking for a simple way to utilize regex in a UNIX shell script where not every system will have perl extensions built into grep. What is really helpful about perl regex here is back/forward ...
9
votes
3answers
1k views
Substituting strings in a very large file
I have a very long series of urls with no separating character, in the same format as below:
http://example.comhttp://example.nethttp://example.orghttp://etc...
I want each URL to be on a new line. ...
2
votes
2answers
370 views
Search and replace with sed
Last week I accidently externalized all my strings of my eclipse project. I need to revert this and my only hope is sed. I tried to create scripts but failed pathetically because I'm new with sed and ...
1
vote
2answers
513 views
To pass a parameter with sed
For my script I have to read the data from a file.
For example I have 2 files: one is a text file, the second is a csv file with 10 columns.
What I am trying to do is to read the csv and change the ...
9
votes
3answers
16k views
What characters do I need to escape when using sed in a sh script?
Take the following script:
#!/bin/sh
sed 's/(127\.0\.1\.1)\s/\1/' [some file]
If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't ...
3
votes
5answers
380 views
changing pattern of a text file
A text file has contents something like
chair
table
pen
desk
Now i want it to be changed and stored in a variable say var as below
('chair','chair'),('table','table'),('pen','pen'),('desk','desk')
...
4
votes
6answers
3k views
How can I convert tab delimited data to comma delimited data?
I'm requesting a list of ec2 snapshots via amazon's ec2 command line tool:
ec2-describe-snapshots -H --hide-tags > snapshots.csv
The data looks something like this:
SnapshotId VolumeId ...
4
votes
4answers
2k views
How to perform a sed in-place substitution that only creates backups of files that were changed?
I ran the following to replace a term used in all files in the current working directory:
$ find . -type f -print0 | xargs -0 sed -i'.bup' -e's/Ms. Johnson/Mrs. Melbin/g'
This performed the word ...
2
votes
2answers
469 views
Adding 2 lines in text file stanza
I have a text file divided in stanzas.
Each stanza starts with a [keyword] and ends with a blank line. Something like this:
[keyword1]
param1 = value1
param2 = value2
param3 = value3
[keyword2]
...
2
votes
2answers
2k views
Strip trailing whitespace from files
The answer to removing trailing whitespace with sed has most of the answer, but I want
sed -i 's/[ \t]*$//' $1
to be able to take arbitrary number of file arguments as a shell script on the command ...
2
votes
1answer
373 views
How do I replace certain text with the respective line number using sed?
I have a file where certain (not all) lines look like so:
Sequence: n
I wish to replace n with the respective line number.
How can I do it with sed?
3
votes
1answer
627 views
Chinese characters instead of Latin being written to file
When I run sed like this and print to a console everything is fine:
sed '/Q/{
s/Q//g
r /Users/ericbrotto/Desktop/question.txt
}' Commision.txt
But when I do this and output t a file:
sed '/Q/{
...
2
votes
1answer
154 views
reading input in sed
i'm new to shell scripting. I'm learning little by little. I'm more familiar with php which is working toward my disadvantage right now.
Anyway, i'm writing a shell script and i read in input with ...