Manipulate and move columns in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Manipulate and move columns in a file
# 1  
Old 08-08-2011
Manipulate and move columns in a file

Hello Unix Gurus,

I have a request 2 perform several functions on a file, delete columns, delete rows based on column value, and finally move around columns in the final output. Consider the following input file with 12 columns;

Code:
NETW~US60~000000000000518000~008~~CS~USD~102.00~20090701~99991231~01~01~20080714
NETW~US60~000000000000518000~SL8~~CS~USD~102.00~20100701~99991231~01~01~20080714
NETW~US61~000000000000772000~008~~CS~USD~118.08~20090701~99991231~01~01~20051012
NETW~US60~000000000000772000~SL8~~CS~USD~118.08~20100701~99991231~01~01~20051012
NETW~US60~000000000000772000~008~~CS~USD~118.08~20100701~99991231~01~01~20051012

1. The first action I want to perform is to delete columns 1, 10, 11, and 12
2. Delete all rows where value in column 1 is not equal to US60 and column 3 is not equal to 008
3. Convert values in column 1 from US60 to A
4. Convert values in column 3 from 008 to SLP
5 Move column 8 to column 4, move column 7 to column 5, move column 4 to column 8, move column 5 to column 7, column 6 stays in the same position.

Such that the output from all this would be:

Code:
A~000000000000518000~SLP~99991231~20090701~102.00~USD~CS~
A~000000000000772000~SLP~99991231~20100701~118.08~USD~CS~

Any script or code that can help achieve this results, will be greatly appreciated.

Kind Regards,

Chukwuma

Last edited by pludi; 08-08-2011 at 03:29 AM..
# 2  
Old 08-08-2011
Try...
Code:
awk 'BEGIN{FS=OFS="~"}$2=="US60"&&$4=="008"{print "A",$3,"SLP",$10,$9,$8,$7,$6,$5,""}' file1 > file2

This User Gave Thanks to Ygor 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

Help - manipulate data by columns and repeated

Hello good afternoon to everyone. I'm new to the forum and would like to request your help in handling data. I hope my English is clear. I have a file (Dato01.txt) to contine the following structure. # Col1 - Col2 - Col3 - Col4 Patricia started Jun 22 05:22:58 Carolina started Jun... (5 Replies)
Discussion started by: kelevra
5 Replies

2. Shell Programming and Scripting

Manipulate the columns of 2 files

Hello, I have two files to be treated. First file: col1 col2 col3 col4 Second file: colbis - I try to add the unique column of the file 2 towards the file 1. - To obtain the following result with a shell script ksh: col1 col2 col3 col4 colbis (4 Replies)
Discussion started by: khalidou13
4 Replies

3. Shell Programming and Scripting

Manipulate the text file in UNIX

Hi All, I have a file like below and i have 2 questions on this (They are 3 lines starts with 01 , 02 and 03. but is 01abc333644554 234 2334535 34534535355353 sfsdf345455 353 4543 jgkg tty 7676 02cdesdfsdfsdf 234 wesdfsdf 345345 234234 234234 2342342 dfgdfg sdfgg dgdgdg fgvfs... (6 Replies)
Discussion started by: siva.pitchai
6 Replies

4. Shell Programming and Scripting

Manipulate a CSV File

Hello, How do i manipulate .csv file to this format? Thank you very much. Source: john,5 marco,7 john,4 paul,3 marco,8 Output: john,9 marco,15 (5 Replies)
Discussion started by: tara123
5 Replies

5. Shell Programming and Scripting

Manipulate file

Hi Guys, I have a file that lists patches along with other information. The patches are listed in two different formats. One format lists the latest patch, date , installed patch Latest Patch Date IN 148412-02 13-Sep-2012 -- X X SunOS 5.10: nss_dns patch 126206-10 ... (5 Replies)
Discussion started by: Tornado
5 Replies

6. Shell Programming and Scripting

Find string in one file and manipulate other

hi, I have 2 files delimited by "|" File 1: 1|28|ABC|11|9620034||XXX555| 29|22|ABC|11|9620258||XXX555| 51|26|ABC|11|9620314||XXX555| 77|20|ABC|11|9630506||XXX555| 97|36|ABC|11|9630562||XXX555| File 2: 9620028|I 9620034|I 9620314|S 9620332|I 9620258|I 9630506|S 9630562|S (3 Replies)
Discussion started by: pparthiv
3 Replies

7. Shell Programming and Scripting

Manipulate columns using sed

Hello, I would like to remove the first column of lines beginning by a character (in my case is an open square bracket) and finishing by a space (or any other delimiter). For example: string1 string2 string3 to string2 string3 I found this previous topic: ... (1 Reply)
Discussion started by: stoyanova
1 Replies

8. Shell Programming and Scripting

Shell script to manipulate a file

Hello, I have a file with following contents : WSL SRVGRP=LISTENER SRVID=2 CLOPT="-A -t -- -n 0x0002aa050a03cc65 " RQPERM=0660 REPLYQ=Y RPPERM=0660 MIN=1 MAX=1 CONV=N I need to print only the value in Hex i.e.... (2 Replies)
Discussion started by: deo_kaustubh
2 Replies

9. Shell Programming and Scripting

Extract File line and manipulate

How can I print a section of each line in a text file. Eg CODE1 XYR Test2 10319389 CODE2 XYR Test2 10319389 CODE3 XYR Test2 10319389 CODE4 XYR Test2 10319389 CODE5 XYR Test2 10319389 First thing that would be nice would a new file like, awk sed and substring may help but can't figure it... (6 Replies)
Discussion started by: kelseyh
6 Replies

10. Filesystems, Disks and Memory

manipulate csv file to add columns

Hi, I have a csv file with a key composed by 3 columns and some other numeric fields and I need to obtain the partial amounts by some part of the key. This may be some difficult to understand, so better see an example, where my input file is: name,surname,department,y2004,y2005,y2006... (6 Replies)
Discussion started by: oscarmon
6 Replies
Login or Register to Ask a Question