Question about 'sed'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question about 'sed'
# 1  
Old 01-29-2008
Question about 'sed'

Hi all:
I understand that the following action:
sed '1s/a/b/' f
will search the first line of f and find the first ocurrence of a then replaced by b. But, instead of specify a character or string, is there any way I can specify the column(s) we want to replace then replace it by using 'sed'? Thanks for help.
# 2  
Old 01-30-2008
not sure about it...but
I think U can only redirect the output ( as a column ) to sed.

it 'd be something like this....

awk '{print $1}' file | sed blah blah
# 3  
Old 01-30-2008
Hi,,
not sure of the content of ur file,,but i tried this on a sample file...

sample file test :-

test1 test2 test3 test4 test5
test1 test2 test3 test4 test5
test1 test2 test3 test4 test5
test1 test2 test3 test4 test5
test1 test2 test3 test4 test5
test1 test2 test3 test4 test5
test1 test2 test3 test4 test5


*******************
script ...
for i in `awk '{print $2}' test | sort -u`
> do
> j=`awk '{print $3}' test | sort -u`
> sed -e 's/'$i'/'$j'/g' test > hi.txt
> done
***********************

output

test1 test3 test3 test4 test5
test1 test3 test3 test4 test5
test1 test3 test3 test4 test5
test1 test3 test3 test4 test5
test1 test3 test3 test4 test5
test1 test3 test3 test4 test5
test1 test3 test3 test4 test5
________________

Not sure if that will solve the purpose..but just an try,,,
# 4  
Old 01-30-2008
Hi.

If you have data that can be described with "fields", then I suggest you immediately think of awk to manipulate that kind of file. It is the best tool for easily dealing with delimited strings.

You can use sed to change the content of specific character positions ("columns"), but it is somewhat cumbersome. For example, suppose you wished to change positions 24-26 and replace those 3 columns with the string "|WXY hs2 Z|". Here is a way to do that:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate substitution in specific character positions.

#  ____
# /
# |   Infrastructure BEGIN

echo
set -o nounset

debug=":"
debug="echo"

## The shebang using "env" line is designed for portability. For
#  higher security, use:
#
#  #!/bin/sh -

## Use local command version for the commands in this demonstration.

set +o nounset
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1)
set -o nounset

echo

FILE=${1-data1}
echo " Input file $FILE:"
cat $FILE

# |   Infrastructure END
# \
#  ---

echo
echo " Results from replacing columns 24 through 26:"
sed  -e 's/^\(.\{23\}\).../\1|WXY hs2 Z|/' data1

exit 0

Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)

 Input file data1:
x        1         2         3         4         5    x
1234567890123456789012345678901234567890123456789012345
Lorem ipsum dolor sit amet, consectetuer adipiscing
elit.  Integer lorem.  Suspendisse sodales suscipit
lectus.  Donec tristique rutrum est.  Phasellus et
odio.  Duis dignissim pretium nibh.  Nunc vehicula,
tellus eget rutrum aliquet, ipsum nisi dictum tortor,
eu adipiscing nisl neque et pede.  Integer sit amet
pede.  Aliquam scelerisque commodo erat.  Cras
tincidunt consequat lectus.  Praesent et nisl.  Mauris
suscipit ipsum in leo.  Cras sit amet mi.

 Results from replacing columns 24 through 26:
x        1         2   |WXY hs2 Z|   3         4         5    x
12345678901234567890123|WXY hs2 Z|78901234567890123456789012345
Lorem ipsum dolor sit a|WXY hs2 Z|, consectetuer adipiscing
elit.  Integer lorem.  |WXY hs2 Z|pendisse sodales suscipit
lectus.  Donec tristiqu|WXY hs2 Z|utrum est.  Phasellus et
odio.  Duis dignissim p|WXY hs2 Z|ium nibh.  Nunc vehicula,
tellus eget rutrum aliq|WXY hs2 Z|, ipsum nisi dictum tortor,
eu adipiscing nisl nequ|WXY hs2 Z|t pede.  Integer sit amet
pede.  Aliquam sceleris|WXY hs2 Z| commodo erat.  Cras
tincidunt consequat lec|WXY hs2 Z|.  Praesent et nisl.  Mauris
suscipit ipsum in leo. |WXY hs2 Z|as sit amet mi.

The "." in the sed pattern means "any character", the "\{23\}" means "previous item repeated 23 times", so you skip over 23 columns, but keep them in a container called "\1" for replacement later. See man sed for more details ... cheers, drl
# 5  
Old 02-21-2008
Replace multiple columns in a row

Hi,

I am new to this forum and what I am looking for is something similar to what is posted above. But, in my case, I have a file with 58 columns (delimited with '|') and 17 columns may come in with a 'space' as a value. So, I need to check these 17 columns for every record and if they have a space, then change it to NULL.

Should I code 17 pieces of awk+replace statements for this purpose? Is there a way to replace multiple fields in a row having a particular value with a different value?

I would appreciate any assistance on this. Thanks guys..!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

SED question

I am trying to write a script that will take an input text file in the format person: place: phonenumber; person: place: phonenumber; person: place: phonenumber; ... and output it using sed too: Name ######## Location ######### Phone Number... (1 Reply)
Discussion started by: jmack56
1 Replies

2. Shell Programming and Scripting

Sed question

I need to replace the numbers with a new string. How can I give a wildcard for the different # of numbers sed '/abcdef/s/abcdef=*/abcdef=999999/'<foo>foo1 From: To: abcdef=1234 abcdef=999999 abcdef=12345 abcdef=999999 abcdef=123456... (10 Replies)
Discussion started by: beppler
10 Replies

3. Shell Programming and Scripting

sed question

hi i have a file with this line: variable=/export/home/oracle I want to change the file so that the path is replaced with the value of another variable var2=/tmp/anything. how to do this in sed? thx (4 Replies)
Discussion started by: melanie_pfefer
4 Replies

4. Shell Programming and Scripting

Sed Question 1. (Don't quite know how to use sed! Thanks)

Write a sed script to extract the year, rank, and stock for the most recent 10 years available in the file top10_mktval.csv, and output in the following format: ------------------------------ YEAR |RANK| STOCK ------------------------------ 2007 | 1 | Exxon... (1 Reply)
Discussion started by: beibeiatNY
1 Replies

5. UNIX for Dummies Questions & Answers

sed question

How would I use sed to print everything on the line after the regular expresion? I have a configuration file setting several variables. cfg.dat DDB = cpptest SUDBNAME = sucpptestdb host = cpptest Example I want to search for the regular expresion 'SUDBNAME =' and print everything on... (3 Replies)
Discussion started by: orahi001
3 Replies

6. Shell Programming and Scripting

sed question

I have a file that conatins following info Policy1=U|guestRoom=test1idCode=5(1):!:Amenity2=U|RoomId=testrma=4(1):!:| GuestRoomAmenity1=U|guestRoomId=testguest1id^rmaCode=5(1):!:| I need it to look like this Policy1=U|guestRoom=test1idCode Amenity2=U|RoomId=testrmaCode... (2 Replies)
Discussion started by: arushunter
2 Replies

7. Shell Programming and Scripting

sed question

Hi, :) can any body explain the following statement sed 's/\(\)- ]//g' cheers RRK (3 Replies)
Discussion started by: ravi raj kumar
3 Replies

8. Shell Programming and Scripting

sed question

Hi, When deleting lines using sed, as i understand the lines are redirected to the standard output. What i'm unclear about is how to actually modify the file? If I write the command sed '1,2d' test it will display lines one and 2 onto the screen however the file is not modified? I think my... (5 Replies)
Discussion started by: c19h28O2
5 Replies

9. Shell Programming and Scripting

sed question (again)

hello there, I have a sed question. I have a file (temp.srv), in it it has v1_host1 v2_host2 And I have another file (temp2.srv), in it is has v1_host3_date v1_host1 v2_host2 v2_host4_date v3_host5_date I had used a script to remove the name from temp2.srv base on the name inside... (3 Replies)
Discussion started by: ahtat99
3 Replies

10. Shell Programming and Scripting

Sed Question

Hi, Is there any way to traverse the file once and look for the following conditions in one sweep instead of going over the file 3 times with different search criteria...... sed -n '/^ORA-07445/ p' /tmp/t$$ > ${OERRFILE} sed -n '/^ORA-00600/ p' /tmp/t$$ >> ${OERRFILE} ... (1 Reply)
Discussion started by: YS2002
1 Replies
Login or Register to Ask a Question