Swapping/replacing fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Swapping/replacing fields
# 1  
Old 09-25-2014
Swapping/replacing fields

Hallo Team,

I would like to replace filed 4 and 7 with filed 39 how can i achieve this ?
Code:
-bash-3.2$ cat dip1.csv| cut -f4,7,24,36,39 -d","|sort -u
+27113996891,+27113996891,196.35.130.52,828854047,+27873500077
+27116452690,+27825702918,10.0.109.13:5060,+27116452690,+27116452690
+27128079817,+27128079817,Group,+27123345534,+27123345534
+27215289900,+27119393742,Group,+27215289900,+27215289900
+27215289900,+27182908474,Group,+27215289900,+27215289900
+27215289900,+27215517696,Group,+27215289900,+27215289900
+27215289900,+27218322200,Group,+27215289900,+27215289900
+27215289900,Unavailable,Group,+27215289900,+27215289900
+27215289912,+27219761935,Group,+27215289912,+27215289912
+27215289915,+27219405911,Group,+27215289915,+27215289915
+27315695024,+27315695024,Group,+27123345582,+27123345560
+27514452472,+27514452472,10.0.108.25:5060,+27514485042,+27873548291
+27722832983,+27722832983,10.0.109.17:5060,+27514485042,+27873548291
+27767269367,+27873548800,196.35.130.52,+27873548800,+27873548800
+27828964576,+27828964576,10.0.108.25:5060,+27873529097,+27873529097
+27873570164,+27832146078,Group,+27873570114,+27873570114
+27873570208,+27832146078,Group,+27873570170,+27873570170
Unavailable,Unavailable,10.0.108.25:5060,+27514485042,+27873548291
Unavailable,Unavailable,10.0.109.17:5060,+27514485042,+27873548291

This is what i did but my method replaces swap the fields it doesnt replace them.
Code:
-bash-3.2$ awk 'BEGIN {FS=OFS=","} {temp=$39; $39=$4; $4=temp} {print}' dip1.csv > Accntfound2.csv
-bash-3.2$ awk 'BEGIN {FS=OFS=","} {temp=$39; $39=$7; $7=temp} {print}' Accntfound2.csv > Accntfound3.csv
-bash-3.2$ cat Accntfound3.csv | cut -f4,7,24,36,39 -d","|sort -u
+27116452690,+27116452690,10.0.109.13:5060,+27116452690,+27825702918
+27123345534,+27128079817,Group,+27123345534,+27128079817
+27123345560,+27315695024,Group,+27123345582,+27315695024
+27215289900,+27215289900,Group,+27215289900,+27119393742
+27215289900,+27215289900,Group,+27215289900,+27182908474
+27215289900,+27215289900,Group,+27215289900,+27215517696
+27215289900,+27215289900,Group,+27215289900,+27218322200
+27215289900,+27215289900,Group,+27215289900,Unavailable
+27215289912,+27215289912,Group,+27215289912,+27219761935
+27215289915,+27215289915,Group,+27215289915,+27219405911
+27873500077,+27113996891,196.35.130.52,828854047,+27113996891
+27873529097,+27828964576,10.0.108.25:5060,+27873529097,+27828964576
+27873548291,+27514452472,10.0.108.25:5060,+27514485042,+27514452472
+27873548291,+27722832983,10.0.109.17:5060,+27514485042,+27722832983
+27873548291,Unavailable,10.0.108.25:5060,+27514485042,Unavailable
+27873548291,Unavailable,10.0.109.17:5060,+27514485042,Unavailable
+27873548800,+27767269367,196.35.130.52,+27873548800,+27873548800
+27873570114,+27873570164,Group,+27873570114,+27832146078
+27873570170,+27873570208,Group,+27873570170,+27832146078

# 2  
Old 09-25-2014
Quote:
Originally Posted by kekanap
Hallo Team,

I would like to replace filed 4 and 7 with filed 39 how can i achieve this ?
Code:
-bash-3.2$ cat dip1.csv| cut -f4,7,24,36,39 -d","|sort -u
+27113996891,+27113996891,196.35.130.52,828854047,+27873500077
+27116452690,+27825702918,10.0.109.13:5060,+27116452690,+27116452690
+27128079817,+27128079817,Group,+27123345534,+27123345534
+27215289900,+27119393742,Group,+27215289900,+27215289900
+27215289900,+27182908474,Group,+27215289900,+27215289900
+27215289900,+27215517696,Group,+27215289900,+27215289900
+27215289900,+27218322200,Group,+27215289900,+27215289900
+27215289900,Unavailable,Group,+27215289900,+27215289900
+27215289912,+27219761935,Group,+27215289912,+27215289912
+27215289915,+27219405911,Group,+27215289915,+27215289915
+27315695024,+27315695024,Group,+27123345582,+27123345560
+27514452472,+27514452472,10.0.108.25:5060,+27514485042,+27873548291
+27722832983,+27722832983,10.0.109.17:5060,+27514485042,+27873548291
+27767269367,+27873548800,196.35.130.52,+27873548800,+27873548800
+27828964576,+27828964576,10.0.108.25:5060,+27873529097,+27873529097
+27873570164,+27832146078,Group,+27873570114,+27873570114
+27873570208,+27832146078,Group,+27873570170,+27873570170
Unavailable,Unavailable,10.0.108.25:5060,+27514485042,+27873548291
Unavailable,Unavailable,10.0.109.17:5060,+27514485042,+27873548291

This is what i did but my method replaces swap the fields it doesnt replace them.
Code:
-bash-3.2$ awk 'BEGIN {FS=OFS=","} {temp=$39; $39=$4; $4=temp} {print}' dip1.csv > Accntfound2.csv
-bash-3.2$ awk 'BEGIN {FS=OFS=","} {temp=$39; $39=$7; $7=temp} {print}' Accntfound2.csv > Accntfound3.csv
-bash-3.2$ cat Accntfound3.csv | cut -f4,7,24,36,39 -d","|sort -u
+27116452690,+27116452690,10.0.109.13:5060,+27116452690,+27825702918
+27123345534,+27128079817,Group,+27123345534,+27128079817
+27123345560,+27315695024,Group,+27123345582,+27315695024
+27215289900,+27215289900,Group,+27215289900,+27119393742
+27215289900,+27215289900,Group,+27215289900,+27182908474
+27215289900,+27215289900,Group,+27215289900,+27215517696
+27215289900,+27215289900,Group,+27215289900,+27218322200
+27215289900,+27215289900,Group,+27215289900,Unavailable
+27215289912,+27215289912,Group,+27215289912,+27219761935
+27215289915,+27215289915,Group,+27215289915,+27219405911
+27873500077,+27113996891,196.35.130.52,828854047,+27113996891
+27873529097,+27828964576,10.0.108.25:5060,+27873529097,+27828964576
+27873548291,+27514452472,10.0.108.25:5060,+27514485042,+27514452472
+27873548291,+27722832983,10.0.109.17:5060,+27514485042,+27722832983
+27873548291,Unavailable,10.0.108.25:5060,+27514485042,Unavailable
+27873548291,Unavailable,10.0.109.17:5060,+27514485042,Unavailable
+27873548800,+27767269367,196.35.130.52,+27873548800,+27873548800
+27873570114,+27873570164,Group,+27873570114,+27832146078
+27873570170,+27873570208,Group,+27873570170,+27832146078

Hello kekanap,

Following may help you.

Code:
awk -F"," '{$4=$7=$39} 1' OFS="," Input_file

NOTE: This is not tested code.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-25-2014
R.Singh........you are a rockster.
This User Gave Thanks to kekanap For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Swapping fields

Hallo Team, This is the command that i am running : grep ",Call Forward Not Reachable" *2013* this is the output that i am getting (i did a head -10 but the files can be more than 1000) ... (8 Replies)
Discussion started by: kekanap
8 Replies

2. Shell Programming and Scripting

Swapping of fields in file

Hi Friends , I have file1.txt 1|b|46|123|47673|348738 2|c|63|124|7346|4783 3|y|45|125|5555|78789 output should swap the 4th field to the first field. output 123|1|b|46|47673|348738 124|2|c|63|7346|4783 125|3|y|45|5555|78789 (3 Replies)
Discussion started by: i150371485
3 Replies

3. Shell Programming and Scripting

AWK swapping fields on different lines

Hi All, Sorry if this question has been posted elsewhere, but I'm hoping someone can help me! Bit of an AWK newbie here, but I'm learning (slowly!) I'm trying to cobble a script together that will save me time (is there any other kind?), to swap two fields (one containing whitespace), with... (5 Replies)
Discussion started by: Bravestarr
5 Replies

4. Shell Programming and Scripting

Replacing certain fields from certain rows

Hi all, say for example i have the next input file 30 Au 7.500000 7.500000 5.774000 Au 7.500000 8.995000 8.363000 Au 7.500000 6.005000 8.363000 Au 20.633000 7.500000 9.226000 Au 20.632000 6.005000 6.637000 Au 20.632000 ... (4 Replies)
Discussion started by: ezitoc
4 Replies

5. Shell Programming and Scripting

Replacing fields

Hi! I have a file somefile.txt: 12, 1, a, b, c, d, e, f 12, 1, a, b, c, d, e, f 17, 51, a, b, c, d, e, f ... I've made this script to read two fields from a line and output a third: cat somefile.txt | awk -F, '{if ($1 == "12" && $2== "1") print "19"; else if ($1 == "17" && $2== "51")... (8 Replies)
Discussion started by: Tr0cken
8 Replies

6. Homework & Coursework Questions

Swapping Fields with Sed

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The assignment is to convert a text table to csv format. I've got the cleaning up done, but I need to swap two... (0 Replies)
Discussion started by: VoiceInADesert
0 Replies

7. Shell Programming and Scripting

Comparing two files and replacing fields

I have two files with ids and email addresses. File 2 cotains a subset of the records in file 1. The key field is the first field containing the id. file 1: 123|myadr@abc.com 456|myadr2@abc.com 789|myadr3@abc.com file 2: 456|adr456@xyz.com Where the record appears in the second... (3 Replies)
Discussion started by: tltroy
3 Replies

8. Shell Programming and Scripting

Swapping of fields in a file

Hi I have afile with 15fields,say f1,f2....f15 delimited on comma. How can i swap the f1,f15 fields using unix shell commands or any script? Thanks (3 Replies)
Discussion started by: ksailesh
3 Replies

9. UNIX for Dummies Questions & Answers

Finding & Replacing specific Fields

All I have a very large file (aproximately 150,000) as shown below separated by pipe "|". I need to replace data in 2, 16, 17, 23 fields that are of time stamp format. My goal is to look in those fields and it ends with "000000|" then replace it with "000|". In other words, make it as 6 digit... (2 Replies)
Discussion started by: ddraj2015
2 Replies

10. Shell Programming and Scripting

Specifying and replacing fields with awk

#cat BATCH007.TXT 01,661060052,061000104,081118,0915,07,80,1,2/ 99,,,2/ I have this file called BATCH007.TXT. I am trying to change fields 2 and 3 on line 2 to have zeroes. Like this: 01,661060052,061000104,081118,0915,07,80,1,2/ 99,0,0,2/ I can use these commands to print identify the... (2 Replies)
Discussion started by: ddurden7
2 Replies
Login or Register to Ask a Question