How to reverse all columns of a file having some field separator?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to reverse all columns of a file having some field separator?
# 1  
Old 02-06-2013
How to reverse all columns of a file having some field separator?

Hello,

I have a file:
xandyandz
Code:
x & y & z
x*y*z*a

I require output as:
zandyandx
Code:
z & y & x
a*z*y*x

here all lines have different field seperator (and & * )based on that i want to reverse the column of a file.

Pl. help.

Last edited by Scrutinizer; 02-06-2013 at 04:12 AM.. Reason: code tags
# 2  
Old 02-06-2013
Code:
perl -F'' -lane 'print reverse @F' file

# 3  
Old 02-06-2013
Code:
rev file

# 4  
Old 02-06-2013
Both the codes are not fulfilling my requirement. Codes are reversing all the alphabets irrespective of field separator.

e.g. my file has
Code:
abc & def & ghi

required output :
Code:
ghi & def & abc

code output :
Code:
cba & fed & ihg

I need code for required output :
Code:
ghi & def & abc


Last edited by vgersh99; 02-06-2013 at 10:43 AM.. Reason: code tags, please!
# 5  
Old 02-06-2013
How many different field separators are you going to have? If that's unknown then it's pretty difficult. Or is there a pattern for field separators? Like field separators in your file are not alphabets and all the fields consist of only alphabets?
# 6  
Old 02-06-2013
Quote:
Originally Posted by nehashine
Both the codes are not fulfilling my requirement. Codes are reversing all the alphabets irrespective of field separator.
It seems your requirement has changed..
Quote:
e.g. my file has
Code:
abc & def & ghi

required output :
Code:
ghi & def & abc

code output :
Code:
cba & fed & ihg

I need code for required output :
Code:
ghi & def & abc

These are two different requirements. The second is actually fullfilled by rev. Which of the two are you looking for?
# 7  
Old 02-06-2013
Try:
Code:
awk -F'[^[:alpha:]]*' '{split($0,S,/[[:alpha:]]*/); s=j=x; for(i=NF; i>=1; i--) s=s S[++j] $i; print s}' file

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

Inserting a field without disturbing field separator on other fields

Hi All, I have the input as below: cat input 032016002 2.891 97.109 16.605 27.172 24.017 32.207 0.233 0.021 39.810 0.077 0.026 19.644 13.882 0.131 11.646 0.102 11.449 76.265 23.735 16.991 83.009 8.840 91.160 0.020 99.980 52.102 47.898 44.004 55.996 39.963 18.625 0.121 1.126 40.189... (15 Replies)
Discussion started by: am24
15 Replies

2. Shell Programming and Scripting

Pivot Rows to Columns, with field separator

Hi All, I have a requirement to flatten data out, based on the value in COL_2. Our file is pipe delimited, however COL_2 contains a comma separated string, which we would like to pivot out from one row into multiple rows. Please see my example input data below: Input Data ... (4 Replies)
Discussion started by: RichZR
4 Replies

3. Shell Programming and Scripting

Field separator

Hello All, I have a file, but I want to separate the file at a particular record with comma"," in the line Input file APPLE6SSAMSUNGS5PRICEPERPIECEDOLLAR600EACH010020340URX581949695US to Output file APPLE6S,SAMSUNGS5,PRICEPERPIECE,DOLLAR600EACH,010020340URX581949695,US This is for... (11 Replies)
Discussion started by: m6248m
11 Replies

4. UNIX for Advanced & Expert Users

Removing special chars from file and maintain field separator

Running SunOs 5.6. Solaris. I've been able to remove all special characters from a fixed length file which appear in the first column but as a result all subsequent columns have shifted to the left by the amount of characters deleted. It is a space separated file. Line 1 in input file is... (6 Replies)
Discussion started by: iffy290
6 Replies

5. UNIX for Dummies Questions & Answers

Add a field separator (comma) inside a line of a CSV file

Hi... I can't find my little red AWK book and it's been a long while since I've awk'd. But I need to take a CSV file and convert the first word of the fifth field to its own field by replacing a space with a comma. This is for importing a spreadsheet of issues into JIRA... Example: a line... (9 Replies)
Discussion started by: Tawpie
9 Replies

6. UNIX for Dummies Questions & Answers

reverse first field from the file

Hi all, I have a file named file1as 07/25 00:10 d327490 07/25 00:55 d378299 07/25 03:58 d378299 07/25 06:14 d642035 07/25 12:44 c997126 and now i want to reverse the first filed ie 07/25 as 25/07 00:10 d327490 25/07 00:55 d378299 25/07 03:58 d378299 25/07 06:14 d642035 25/07... (5 Replies)
Discussion started by: zozoo
5 Replies

7. UNIX for Dummies Questions & Answers

change field separator only from nth field until NF

Hi ! input: 111|222|333|aaa|bbb|ccc 999|888|777|nnn|kkk 444|666|555|eee|ttt|ooo|ppp With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records). In order to get: 111|222|333|aaa; bbb; ccc 999|888|777|nnn; kkk... (1 Reply)
Discussion started by: beca123456
1 Replies

8. Shell Programming and Scripting

Field separator X'1F'

Hi, I have a flat file with fields separated by a X'1F' i have to fetch 4th field from second line. please help me how to achieve it. I tried with below command and its not working. cut -f4 -d`echo -e '\x1f'` filename.txt I am using SunOS. Thanks in advance. (2 Replies)
Discussion started by: rohan10k
2 Replies

9. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

10. Shell Programming and Scripting

Help in formating a file with field separator

hi thanks a lot But pls help me if my input file is the below format: Name priya 2010-09-21 10:43:49 TEXT ID 1 hi TEXT ID 2 how TEXT ID 3 r TEXT ID 4 u Name2 priya2 2010-09-21 10:43:49 TEXT ID 1 hi1 TEXT ID 2 how1 TEXT ID 3 r1 (4 Replies)
Discussion started by: bha148
4 Replies
Login or Register to Ask a Question