Cut 2 fields and write to a output file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut 2 fields and write to a output file
# 1  
Old 02-09-2010
Cut 2 fields and write to a output file

Hi,

I am writing a code where the file is a pipe delimited and I would need to extract the 2nd part of field2 if it is "ATTN", "C/O" or "%" and check to see if field9 is populated or not. If field9 is already populated then leave it as is but if field9 is not populated then take the 2nd part of data from field2 (if ATTN, C/O or %) and move it to field9.

I have the code written but as I am reading 1 line at a time it is taking a very very long time to write the output. I would really appreciate if I can get some help on it. The code is below.

For Example :

Code:
 
Example Data
 
Input File : 
               Field2                                        Field9
Record 1 : 123 Main St, ATTN : James Bond |John Smith 
Record 2 : 199 6th St, ATTN: Mary Jones | 
 
Output File : 
               Field2                                        Field9
Record 1 : 123 Main St,                        |John Smith 
Record 2 : 199 6th St,                         | Mary Jones 
 
 
 
cat test_file.dat | while read line
do
temp_field9=`echo $line | awk 'BEGIN{FS="|"}{print $9}END { }'`
temp_attn=`echo $line | awk 'BEGIN{FS="ATTN"}{print $2}END { }' | awk 'BEGIN{FS="|"}{print $1}END { }'`
temp_co=`echo $line | awk 'BEGIN{FS="C/O"}{print $2}END { }' | awk 'BEGIN{FS="|"}{print $1}END { }'`
temp_pct=`echo $line | awk 'BEGIN{FS="%"}{print $2}END { }' | awk 'BEGIN{FS="|"}{print $1}END { }'`
if ( [ ! $temp_field9 ] ) then
if ( [ $temp_attn ] ) then
temp_field9=`echo $temp_attn`
elif ( [ $temp_co ] ) then
temp_field9=`echo $temp_co`
elif ( [ $temp_pct ] ) then
temp_field9=`echo $temp_pct`
fi
fi
if ( [ $temp_attn ] ) then
field2=`echo $temp_field2 | awk 'BEGIN{FS="ATTN"}{print $1}END { }'`
echo $field2> ${STP_HOME_DIR}/temp_company.dat
elif ( [ $temp_co ] ) then
field2=`echo $temp_field2 | awk 'BEGIN{FS="C/O"}{print $1}END { }'`
echo $field2> ${STP_HOME_DIR}/temp_company.dat
elif ( [ $temp_pct ] ) then
field2=`echo $temp_field2 | awk 'BEGIN{FS="%"}{print $1}END { }'`
echo $field2> ${STP_HOME_DIR}/temp_company.dat
else
echo $line | awk 'BEGIN{FS="|"}{print $2}END { }'> ${STP_HOME_DIR}/temp_company.dat
fi
echo $field2"|"$temp_field9>>  new_test_file.dat
done

Please advise.

Thanks,...
# 2  
Old 02-09-2010
The desc is not so clear enough on $9 rules. Please explain more detail.

Please provide sample with all date fields.

Here is draft code for you.

Code:
$  cat input.txt
Record 1 : 123 Main St, ATTN : James Bond |John Smith ::::::
Record 2 : 199 6th St, C/O: Mary Jones | James :::::::::

bwa89@tcs-c90f903ef65 ~/temp
$ aawk -F ":" '{split($2,a,",")} {if (a[2]~/ATTN|C\/O|%/) {$2=a[1]",";split($3,b,"|");$3=""; $9="|"b[2]}}1' OFS=":" input.txt
Record 1 : 123 Main St,:::::::|John Smith
Record 2 : 199 6th St,:::::::| James :::


Last edited by rdcwayx; 02-09-2010 at 09:26 PM..
# 3  
Old 02-10-2010
If field9 already have data populated then we need to leave the data "As Is". Else we take 2nd part of data from field2, if field2 have ATTN, C/O or %, and move it to field9 on output.

Thanks....

Code:
 
For Example :
 
Input Record : 
 
Record 1 : 123 Main St ATTN  James Bond |John Smith 
Record 2 : 199 6th St C/O Mary Jones |
 
Output should be : 
Record 1 : 123 Main St |John Smith 
Record 2 : 199 6th St| Mary Jones

# 4  
Old 02-24-2010
see if you want to extract your change your given input to your out put use this sed command.

Example:


Code:
sed -e  " s/ATTN \| C\/O/\|/g; s/\([A-Z][a-z]* [A-Z][a-z]*\) |\([A-Z][a-z]* [A-Z][a-z]*\)/\1/g;s/[|]$//g "; fine-name

Out put:

Record 1 : 123 Main St | James Bond
Record 2 : 199 6th St| Mary Jones
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

2. Shell Programming and Scripting

Using to perl to output specific fields to one file

Trying to use perl to output specific fields from all text files in a directory to one new file. Each text file on a new line. The below seems to work for one text file but not more. Thank you :). perl -ne 's/^#//; @n = (6, 7, 8, 16); print if $. ~~ @n' *.txt > out.txt format of all text... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. UNIX for Dummies Questions & Answers

Write 2nd and 3rd fields to a 4th file name?

I have a flatfile A.txt date|products|notes|location 121117|a108|this is a test|florida 121118|b111|just test it|tampa How do i write an awk to create a file name as location.txt and have products:notes awk -F'|' '{ print $2 ":" $3 }' A.txt > $4.txt I am sure it cannot write to... (5 Replies)
Discussion started by: sabercats
5 Replies

4. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

5. Shell Programming and Scripting

Cut fields from /etc/passwd file into variables?

The script must ask the user to enter the user name and check whether the user exists in /etc/passwd (you must allow the partial usernames also). If the username exists, display the details as: List of users Login Name: User ID: ... (3 Replies)
Discussion started by: nobletechnology
3 Replies

6. Shell Programming and Scripting

how to cut fields in file

Hi, I have data in following format. 10001, John, Daves, Architecture, -2219 10002, Jim, Cirners, Businessman, -2219 1003, Tom, Katch, Engineer, -14003 I want to select the last column of the above given file and paste it on a different file in the following manner. File TEST column... (11 Replies)
Discussion started by: kam786sim
11 Replies

7. Shell Programming and Scripting

output to write in a file

hi all, when i try to start an applicationserver for an example: ./kStart.sh > out.txt so kStart.sh script will start the application server and write the details to out.txt but in mycase... it is writing to out.txt and as well it is showing in the prompt also... I want only the... (2 Replies)
Discussion started by: raghur77
2 Replies

8. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

9. UNIX for Dummies Questions & Answers

Cut specific fields from a file containing multiline records

Hi, I am looking for a method to get column13 to column 50 data from the 1st line of a multiline reord. The records are stored in a large file and are separated by newline. sample format is (data in red is to be extracted) <header> A001dfhskhfkdsh hajfhksdhfjh... (3 Replies)
Discussion started by: sunayana3112
3 Replies

10. Shell Programming and Scripting

Write process output to a file

When I run <ls -l> to get a list of all the files, I want the displayed result to be written to a text file. Is there a way to do that? (1 Reply)
Discussion started by: kn.naresh
1 Replies
Login or Register to Ask a Question