deplace field delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting deplace field delimiter
# 1  
Old 07-22-2010
deplace field delimiter

hi

here my problem:

i have 2 file:

1.tmp

Code:
111
222
555

2.tmp

Code:
1*TEST1**111*LA
2*TEST2**112*LA
3*TEST3**222*LA
4*TEST4**333*LA
5*TEST5**555*LA

output

Code:
1*TEST1*111**LA
2*TEST2**112*LA
3*TEST3*222**LA
4*TEST4**333*LA
5*TEST5*555**LA

thank's
# 2  
Old 07-22-2010
Code:
$ awk -F\* 'BEGIN {OFS = FS} NR == FNR { A[$0]=1; next } A[$4] { X=$4; $4=$3; $3=X }1 ' 1.tmp 2.tmp
1*TEST1*111**LA
2*TEST2**112*LA
3*TEST3*222**LA
4*TEST4**333*LA
5*TEST5*555**LA

Use nawk or /usr/xpg4/bin/awk on Solaris.
This User Gave Thanks to Scott For This Post:
# 3  
Old 07-22-2010
Ty scottn works like a gem

Can you explain me your code
# 4  
Old 07-22-2010
Hi.

Sure.

It reads the first input file
Code:
... } A[$4] { X=$4; $4=$3; $3=X }1 ' 1.tmp 2.tmp

and stores it's values in an array.

Code:
NR == FNR { A[$0]=1; next }

Then it reads the second input file
Code:
... } A[$4] { X=$4; $4=$3; $3=X }1 ' 1.tmp 2.tmp

and checks if field 4 is in the array from the first file. If it is, it swaps fields 3 and 4.

Code:
A[$4] { X=$4; $4=$3; $3=X }1

Then it prints the line
Code:
A[$4] { X=$4; $4=$3; $3=X }1

The awk man page can give you more details.
# 5  
Old 07-22-2010
scottn Smilie
deplace field delimiter-thank-youjpg
# 6  
Old 07-22-2010
Ha ha, that's nice. Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can awk ignore the field delimiter like comma inside a field?

We have a csv file as mentioned below and the requirement is to change the date format in file as mentioned below. Current file (file.csv) ---------------------- empname,date_of_join,dept,date_of_resignation ram,08/09/2015,sales,21/06/2016 "akash,sahu",08/10/2015,IT,21/07/2016 ... (6 Replies)
Discussion started by: gopal.biswal
6 Replies

2. Shell Programming and Scripting

awk :how to change delimiter without giving all field name

Hi Experts, i need to change delimiter from tab to "," sample test file cat test A0000368 A29938511 072569352 5 Any 2 for £1.00 BUTCHERS|CAT FOOD|400G Sep 12 2012 12:00AM Jan 5 2014 11:59PM Sep 7 2012 12:00AM M 2.000 group 5 ... (2 Replies)
Discussion started by: Lakshman_Gupta
2 Replies

3. Shell Programming and Scripting

awk output field delimiter

Dear All, 1.txt (tab in between each value in a line) a b c a b c a c d you can see below, why with ~ i can output with tab, but = cannot? # awk -F'\t' '$2 ~ /b/' 1 a b c a b c # awk -F'\t' '$2 = "b"' 1 a b c a b c a b d ... (1 Reply)
Discussion started by: jimmy_y
1 Replies

4. UNIX for Dummies Questions & Answers

Cut a field from a line having quotes as delimiter

Hi , I have extract a single field from the 2nd row of a file of which the format is as given below "Field1","Field2","Field3",...,"Field17",... I need to cut Field17 as such (no quotes required).. I give the command head -2 file_name | tail -1 | cut -d "," -f17 But the output is... (2 Replies)
Discussion started by: nivin_govindan
2 Replies

5. Shell Programming and Scripting

Problem in extraction when space is a field delimiter

I have more than 1000 files to parse. Each file contains few lines (number of lines varies) followed by a header line having all column's name (SPOT, NAME etc) and then values for those columns. **Example File: sdgafh dfhaadfha sfgaf dhah jkthdj SPOT NAME GENE_NAME CH_MEAN CHDN_MED ... (11 Replies)
Discussion started by: AshwaniSharma09
11 Replies

6. Shell Programming and Scripting

Add field delimiter for the last field

I have a file with three fields and field delimiter '|' like: abc|12:13:45|123 xyz|12:87:32| qwe|54:21:09 In the file the 1st line has proper data -> abc|12:13:45|123 ,the 2nd line doesnt has data for the 3rd field which is okay , the 3rd line doesnt has data for the 3rd field as well the... (5 Replies)
Discussion started by: mehimadri
5 Replies

7. UNIX for Advanced & Expert Users

Printing Field with Delimiter in AWK/cut

Hello, I had posted earlier about printing fields using AWK, but now I have a slightly different problem. I have text files in the format: 1*2,3,4,5 and wish to print the first, third, and fifth fields, including the asterisk and commas. In other words, after filtering it should look... (1 Reply)
Discussion started by: Jahn
1 Replies

8. Shell Programming and Scripting

delimiter appears in field

The typical line of the input file is as follows, 123|abcde|"xyz|mn"|ghelosa|3455hello| The delimiter is |. I need to change it to another delimiter, say ~. For the above line, the output should be: 123~abcde~xyz|mn~ghelosa~3455hello~ The challenge is when | appears in a field, it... (2 Replies)
Discussion started by: derekxu
2 Replies

9. Shell Programming and Scripting

help with sed to add delimiter and new field to each row

I have a file with millions of rows that I need to add a delimiter and a new field with a zero to the end of each row. (its too big to open and do a find and replace regex) I'm looking for the next line '\n' and need to replace it with a Unit Separator (hex \037) 0 \n. I've tried the... (2 Replies)
Discussion started by: kmac
2 Replies

10. Shell Programming and Scripting

delete a field along with delimiter in the whole file

I have file with 20 fields and its pipe delimiter. I need to remove the 18th field along with pipe delimiter that seperates 17th and 18th field. In turn that means i want to make it now a file with only 19 fields. Can some body let me know how ican remove the 18th field from the whole file? (5 Replies)
Discussion started by: dsravan
5 Replies
Login or Register to Ask a Question