Moving a column across a delimited data file


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving a column across a delimited data file
# 1  
Moving a column across a delimited data file

Hi,
I am trying to move a column from one position to another position in a delimited file. The positions are dynamic in nature and are available by environmental variables. Also the file can have n number of columns.

Example:
Initial Column Position=1
Final Column Position=3
Delimiter='|'

Input:
Code:
Col1|Col2|Col3|Col4|Col5
a1|b1|c1|d1|e1
a2|b2|c2|d2|e2
a3|b3|c3|d3|e3

Output:

Code:
Col3|Col2|Col1|Col4|Col5
c1|b1|a1|d1|e1
c2|b2|a2|d2|e2
c3|b3|a3|d3|e3

Please let me know if anybody has any solution to solve this problem.
Note: I am using KSH.
Thanks.

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 10-09-2011 at 11:50 AM..
# 2  
try using awk, it's the one command I use when working with delimited text files. a search of these forums found the following.
https://www.unix.com/unix-advanced-ex...ed-output.html
# 3  
Hi ayan153,

Try:

Code:
$ cat infile 
Col1|Col2|Col3|Col4|Col5
a1|b1|c1|d1|e1
a2|b2|c2|d2|e2
a3|b3|c3|d3|e3
$ awk -v icp=1 -v fcp=3 -v d="|" 'BEGIN { FS=OFS=d } { column=$fcp; $fcp=$icp; $icp=column; print }' infile 
Col3|Col2|Col1|Col4|Col5
c1|b1|a1|d1|e1
c2|b2|a2|d2|e2
c3|b3|a3|d3|e3

Regards,
Birei
This User Gave Thanks to birei For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #220
Difficulty: Easy
When multiple routers are used in interconnected networks, the routers can exchange information about destination addresses using a routing protocol.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with moving list of data to 2nd column of HTML file

Hi Team, Can you help me with writing shell script to printing the list output to 2nd column in HTML file. (2 Replies)
Discussion started by: veereshshenoy
2 Replies

2. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

3. Shell Programming and Scripting

Append data to first column delimited file

Hi, I have a data like Input: 12||34|56|78 Output: XYZ|12||34|56|78 I tried like this , but it puts it on another line awk -F "|" ' BEGIN {"XYZ"} {print $0} 'file Any quick suggessitons in sed/awk ? am using HP-UX (3 Replies)
Discussion started by: selvankj
3 Replies

4. UNIX for Dummies Questions & Answers

Cut the two column from non delimited file

Hi, I have a question how to cut the column from non delimited file This is my file gene_id ENSG00000223972 transcript_id ENST00000456328 exon_number 1 gene_biotype pseudogene gene_name DDX11L1 transcript_name DDX11L1-002 tss_id TSS26614 gene_id ENSG00000223972 transcript_id ENST00000515242... (1 Reply)
Discussion started by: Wan Fahmi
1 Replies

5. Shell Programming and Scripting

Extract second column tab delimited file

I have a file which looks like this: 73450 articles and news developmental psychology 2006-03-30 16:22:40 1 http://www.usnews.com 73450 articles and news developmental psychology 2006-03-30 16:22:40 2 http://www.apa.org 73450 articles and news developmental psychology 2006-03-30... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

6. Shell Programming and Scripting

using awk to substitute data in a column delimited text file

using awk to substitute data in a column delimited text file hello i would like to use awk to do the following calculation from the following snippet. input file C;2390 ;CV BOUILLOTTE 2L 2FACES NERVUREES ;1.00 ;3552612239004;13417 ;25 ;50 ; 12;50000 ; ; ... (3 Replies)
Discussion started by: iindie
3 Replies

7. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

8. UNIX for Dummies Questions & Answers

Extract records by column value - file non-delimited

the data in my file is has no delimiters. it looks like this: H52082320024740010PH333200612290000930 0.0020080131 D5208232002474000120070306200703060580T1502 TT 1.00 H52082320029180003PH333200702150001 30 100.0020080205 D5208232002918000120070726200707260580T1502 ... (3 Replies)
Discussion started by: jclanc8
3 Replies

9. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

10. Shell Programming and Scripting

Get data from delimited file

Hi, I am quite new to shell scripting in Linux. Please help me in solving the given problem. I have file called Data.txt File "Data.txt" looks like India|9990000|City England|888800|London Nigeria|9887766|uganda I need to get the data after first pipe till second pipe into file... (2 Replies)
Discussion started by: ashu_r2001
2 Replies

Featured Tech Videos