Conditional edit for a field using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional edit for a field using sed
# 1  
Old 06-23-2009
Data Conditional edit for a field using sed

Hi

I want to repalce a field in a txt file on solaris with say 100 records and each record having a total of 10 fields separated by a ~ .

based on the following condition the record should be edited or else the record should be written as it is to a

if the seventh field is 'XX' and if length of eight feild is 15
then eight field should be appended by '000'

The new file should have the records which matched the condition and were edited and also the records which were not edited.

Can anybody help me with a sed command to do the same?
# 2  
Old 06-23-2009
Code:
nawk -F'~' '$7 == "XX" && length($8) == 15 {$8=$8 "000"}1' OFS='~' myFile > myFileNew

# 3  
Old 06-24-2009
Thanks !!! that works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed conditional \n replace for each line

How could be removed \n only if appearing at position 80 in the line? (4 Replies)
Discussion started by: RomanF
4 Replies

2. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

3. Shell Programming and Scripting

sed to replace a field from a line with another field

i have something like this, cat filename.txt hui this si s"dfgdfg" omeone ipaddress="10.19.123.104" wel hope this works i want to replace only 10.19.123.104 with different ip say 10.19.123.103 i tried this sed -i "s/'ipaddress'/'ipaddress=10.19.123.103'/g" filename.txt ... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Shell Programming and Scripting

awk or sed? change field conditional on key match

Hi. I'd appreciate if I can get some direction in this issue to get me going. Datafile1: -About 4000 records, I have to update field#4 in selected records based on a match in the key field (Field#1). -Field #1 is the key field (servername) . # of Fields may vary # comment server1 bbb ccc... (2 Replies)
Discussion started by: RascalHoudi
2 Replies

5. Shell Programming and Scripting

edit field using sed or awk

please help me to edit the second field using awk or sed i have input file below aa1001 000001 bb1002 000002 cc1003 000003 so i want the output file like below aa1001 01 bb1002 02 cc1003 03 (38 Replies)
Discussion started by: zulabc
38 Replies

6. Shell Programming and Scripting

awk conditional expression to compare field number and variable value

Hi, I'm trying to compare the value in a field to the value in a variable using awk. This works: awk '$7 == "101"'but this is what I want (and it doesn't work): value=101 awk '$7 == "$value"' Any help or insight on this would be great. Thanks in advance. (1 Reply)
Discussion started by: goodbenito
1 Replies

7. Shell Programming and Scripting

Sed conditional replace

Given this row: |lastname1|middlename1|firstname1|lastname2|middlename2|firstname2 produce this result: |lastname|middlename|firstname where the resultant names are based on the presence of the #2 names above. I.e., if a #2 name is passed (usually will be null,) use that - otherwise... (8 Replies)
Discussion started by: tiggyboo
8 Replies

8. Shell Programming and Scripting

Conditional tab replacement sed/awk

Hi I am struggling to find a solutions to this problem: I have a directory full of files and I wish to: read each line of each file and if any one line in those files is longer than 72 characters I want to replace any tab characters with a space character. Ive been... (3 Replies)
Discussion started by: benackland
3 Replies

9. Shell Programming and Scripting

Edit field count result

Hi all, I'm trying to count the fields in a file and I'm not getting back the desired result. I was wondering if anyone can help. This is the code: #!/bin/bash a=$(awk '{print NF}' foobar) if then echo "this works" else echo "no it's $a instead" fi The foobar file contains... (1 Reply)
Discussion started by: nistleloy
1 Replies

10. Shell Programming and Scripting

sed conditional string replace for each line

Hi all, I appreciate the enormous amount of knowledge that flows in this forum. I am an average UNIX user. I have many files with lines like the below. I have separated each line with space for ease of reading. I need to replace the first occurance of "/00" with null on those lines that have... (6 Replies)
Discussion started by: Nanu_Manju
6 Replies
Login or Register to Ask a Question