Sed command with characters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sed command with characters
# 1  
Old 04-25-2007
Sed command with characters

Hi everyone, I hope that someone can help me out:

In the following line:

PRIHDR;2809999444;2007 6.1;20070423;9;;4021473000003;;4021473000003;8714231530688;EUR;;

The output should be:

PRIHDR;2809999444;2007 6.1;20070423;4;;4021473000003;;4021473000003;8714231530688;EUR;;

I've tried some variations on the following command:

sed 's/;9;;/;4;;' <oldfile>newfile

When I run this command it tells met what to do with the ";" how can I solve this issue?

Thanks in advance,

IHulky

p.s. the other lines in the document (line 2 and further) have to be copied as well to the new file.

Last edited by IHulky; 04-25-2007 at 08:57 PM..
# 2  
Old 04-25-2007
you can use awk? say you only have that line in a file
Code:
# awk -F ";" 'OFS=";"{$5=4;print}' file
PRIHDR;2809999444;2007 6.1;20070423;4;;4021473000003;;4021473000003;8714231530688;EUR;;

# 3  
Old 04-25-2007
Quote:
Originally Posted by ghostdog74
you can use awk? say you only have that line in a file
Code:
# awk -F ";" 'OFS=";"{$5=4;print}' file
PRIHDR;2809999444;2007 6.1;20070423;4;;4021473000003;;4021473000003;8714231530688;EUR;;

That worked perfectly thanks, can you tell me how to keep the command on the first line. In the lines below some characters have been altered to 4.

If it is any helpfull only the first line starts with PRIHDR

Thanks
# 4  
Old 04-25-2007
Code:
awk -F ";" 'BEGIN {OFS = ";"} /^PRIHDR/{$5=4;print}  ' file

# 5  
Old 04-25-2007
Have to test it further, when I run the command it prints the correct output on screen, although the file itself isn't altered.

Thanks for the info, have to test it further tomorrow as it is 02:30 in the morning here in the Netherlands and work starts at 08:00..

If you have any idea's I love to hear them.

Thanks again
# 6  
Old 04-25-2007
awk does not alter files, unless you ask it to. you can just redirect the output to another file using the redirect sign ( "awk command" > anotherfile)
# 7  
Old 04-26-2007
Quote:
Originally Posted by IHulky
Hi everyone, I hope that someone can help me out:

In the following line:

PRIHDR;2809999444;2007 6.1;20070423;9;;4021473000003;;4021473000003;8714231530688;EUR;;

The output should be:

PRIHDR;2809999444;2007 6.1;20070423;4;;4021473000003;;4021473000003;8714231530688;EUR;;

I've tried some variations on the following command:

sed 's/;9;;/;4;;' <oldfile>newfile

When I run this command it tells met what to do with the ";" how can I solve this issue?

Thanks in advance,

IHulky

p.s. the other lines in the document (line 2 and further) have to be copied as well to the new file.
Add slash
Code:
sed '/^PRIHDR/s/;9;;/;4;;/' <oldfile >newfile
mv newfile oldfile

Code:
perl -i -ne ' s/;9;;/;4;;/ if ( /^PRIHDR/ ); print ' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Outputting characters after a given string and reporting the characters in the row below --sed

I have this fastq file: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGGGGGGGGGGGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCA +test-1 GGGGGGGGGGGGGGGGGCCGGGGGFF,EDFFGEDFG,@DGGCGGEGGG7DCGGGF68CGFFFGGGG@CGDGFFDFEFEFF:30CGAFFDFEFF8CAF;;8... (10 Replies)
Discussion started by: Xterra
10 Replies

2. Shell Programming and Scripting

sed command on AIX, replace specific characters

Hi, Im using sed on an AIX machine. I am trying to change the 137-139 characters if they are a ' 36'/'000' to a '036'. The positions that need to be changed are fixed. the source data that I have is$cat v.txt 4000422985400050462239065593606500000007422985707771046154054910075641MC0318AMWAY... (9 Replies)
Discussion started by: dsid
9 Replies

3. Shell Programming and Scripting

Tcsh script - sed command for special characters

Hi, I am using sed command to replace following line in gz file- sed -n 's/""COS_12_TM_1" ( 1101110101001001010011110100000010110100010010000000100000000010XX010000000 )"/""COS_12_TM_1" ( 110111010100100101001111MM00000010110100010010000000100000000010XX010000000 )"/g' filename.gz $x=... (4 Replies)
Discussion started by: Preeti Chandra
4 Replies

4. Shell Programming and Scripting

Sed: -e expression #1, char 2: extra characters after command

Greetings.. getting the error while execution of the script, correct where i am missing #!/bin/bash DATE=`date +%Y-%m-%d:::%H:%M` HOSTNAME=`hostname` TXT="/log/temp.txt" LOGPATH="/log1/commanlogs/" IP=`/sbin/ifconfig | grep -i inet| head -n1| awk '{print $2}'| awk -F : '{print $2}'`... (7 Replies)
Discussion started by: manju98458
7 Replies

5. Shell Programming and Scripting

Help with sed command - find a string between two characters

Hi, I have a xml file (Config.xml) <Header name="" TDate="" PDate=""> <Config> {"config" { "Nation" "Pri:|Sec:"}} </Config> </Header> Now I wanted to printed all the strings between "". I tried the following cat Config.xml | sed -n 's/.*\.*//p' ... (8 Replies)
Discussion started by: vivek_damodaran
8 Replies

6. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

7. UNIX for Dummies Questions & Answers

sed command to remove characters help!

I am trying to analyse a large file of sequencing data, example of first 10 lines below, @HWUSI-EAS656_0044_FC:7:1:2447:1039#GCAATT/1 GNCTATGGCTTGCCGGGCTCAGGGAAGACAATCATAGCCATGAAAATCATGGAAAAGATCAGAAAAACATTTCAA +HWUSI-EAS656_0044_FC:7:1:2447:1039#GCAATT/1... (1 Reply)
Discussion started by: Adeleh
1 Replies

8. Shell Programming and Scripting

Counting characters with sed

Input: ghw//yw/hw///??u How can i count the slashes("/") using sed? (13 Replies)
Discussion started by: cola
13 Replies

9. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

10. Shell Programming and Scripting

sed with many special characters

I started with this: counter1=1 cp file.txt file_${counter1}.tmp while read name1 do echo $name1 counter2=`expr $counter1 + 1` sed /'${name1}'/d file_${counter1}.txt > file_${counter2}.txt counter1=`expr $counter1 + 1` done < source.txtsource.txt contains the... (1 Reply)
Discussion started by: lakanino
1 Replies
Login or Register to Ask a Question