Change Vertical to Horizontal


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Change Vertical to Horizontal
# 1  
Old 02-06-2015
Change Vertical to Horizontal

I need to change data from vertical to horizontal but with condition

input
Code:
USA|80
AUS|40
BRA|33

VEGAS|40
KENTUCKY|50
NEWYORK|21
DARWIN|33
ADELAIDE|21
SAOPAOLO|44
RIO|89
GAPIZA|44
BENFLEX|32

AXIS|44
ACRE|56
HEIGHT|22
LENGTH|21

I need to arrange my data in fix status
Code:
USA|80,VEGAS|40,KENTUCKY|50,NEWYORK|21,AXIS|44,ACRE|56
AUS|40,DARWIN|33,ADELAIDE|21,HEIGHT|22
BRA|33,SAOPAOLO|44,RIO|89,GAPIZA|44,BENFLEX|32,LENGTH|21

USA is always arrange with VEGAS,KENTUCKY,NEWYORK,AXIS,ACRE and so does AUS and BRA

I did
Code:
paste -d, -s input

but its only arrange file in one line
Please help

Last edited by radius; 02-06-2015 at 04:51 AM.. Reason: First column separated by "|" is fixed while its value changes daily basis
# 2  
Old 02-06-2015
Hello radius,

Could you please try following it may help you. You would have given some more conditions(if any) so make it easier.
Code:
awk -F"|" '{if($1 ~ /USA/ || $1 ~ /VEGAS/ || $1 ~ /KENTUCKY/ || $1 ~ /NEWYORK/ || $1 ~ /AXIS/ || $1 ~ /ACRE/){A=A?A OFS $0:$0}} {if($1 ~ /AUS/ || $1 ~ /DARWIN/ || $1 ~ /ADELAIDE/ || $1 ~ /HEIGHT/){B=B?B OFS $0:$0}} {if($1 ~ /BRA/ || $1 ~ /SAOPAOLO/ || $1 ~ /RIO/ || $1 ~ /GAPIZA/ || $1 ~ /BENFLEX/ || $1 ~ /LENGTH/){C=C?C OFS $0:$0}} END{print A ORS B ORS C}' OFS=,  Input_file

Output will be as follows.
Code:
USA|80,VEGAS|40,KENTUCKY|50,NEWYORK|21,AXIS|44,ACRE|56
AUS|40,DARWIN|33,ADELAIDE|21,HEIGHT|22
BRA|33,SAOPAOLO|44,RIO|89,GAPIZA|44,BENFLEX|32,LENGTH|21

Thanks,
R. Singh
# 3  
Old 02-06-2015
I did this as an example
Code:
awk -F"|" '$1~"USA" || $1~"VEGAS" {print $0}' sss.txt | paste -d, -s

is there any other shortest way?

---------- Post updated at 04:13 AM ---------- Previous update was at 04:11 AM ----------

Code:
C=C?C OFS $0:$0}} END{print A ORS B ORS C}'

what does this mean Mr Rav?
# 4  
Old 02-06-2015
Hello radius,

Following is the explaination.
Code:
if($1 ~ /USA/ || $1 ~ /VEGAS/ || $1 ~ /KENTUCKY/ || $1 ~ /NEWYORK/ || $1 ~ /AXIS/ || $1 ~ /ACRE/){A=A?A OFS $0:$0}
If $1 is equal to USA or VEGAS or KENTUCKY or NEWYORK or AXIS or ACRE  then I am concatinating the values of the line into a single variable named A and when all values collected in A at LAST I am printing it by using END.
Similarly with variable B and C too.

Hope this helps.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 02-06-2015
thanks..it helps
# 6  
Old 02-06-2015
OR try something like this

Code:
[akshay@localhost tmp]$ cat infile
USA|80
AUS|40
BRA|33

VEGAS|40
KENTUCKY|50
NEWYORK|21
DARWIN|33
ADELAIDE|21
SAOPAOLO|44
RIO|89
GAPIZA|44
BENFLEX|32

AXIS|44
ACRE|56
HEIGHT|22
LENGTH|21

Code:
[akshay@localhost tmp]$ cat group.sh
#!/usr/bin/env bash

tempf=$(mktemp)

cat >$tempf<<EOF
USA|VEGAS|KENTUCKY|NEWYORK|AXIS|ACRE
AUS|DARWIN|ADELAIDE|HEIGHT
BRA|SAOPAOLO|RIO|GAPIZA|BENFLEX|LENGTH
EOF


awk  'FNR==NR{
		    A[FNR] = $0 
		    next 
           }
	   {
		for(i in A)
		{
			if($1 ~ A[i])
			{
				B[i] =  i in B ? B[i] OFS $0 : $0  
			}	
		}
	   }
	END{
		for(i in B)print B[i]
	   }
      ' FS="|" OFS="," "$tempf"  infile

rm $tempf

Code:
[akshay@localhost tmp]$ chmod +x group.sh

Code:
[akshay@localhost tmp]$ ./group.sh 
USA|80,VEGAS|40,KENTUCKY|50,NEWYORK|21,AXIS|44,ACRE|56
AUS|40,DARWIN|33,ADELAIDE|21,HEIGHT|22
BRA|33,SAOPAOLO|44,RIO|89,GAPIZA|44,BENFLEX|32,LENGTH|21

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

Need to move vertical to horizontal using paste

Source file Name:hostname1 Masking views : Yes Storage Group Names : hostname1 device (5): Name:hostname2 Masking views : Yes Storage Group Names : hostname2 device (5): Name:hostname3 Masking views : no Storage Group Names : hostname3 device (5):... (9 Replies)
Discussion started by: ranjancom2000
9 Replies

2. UNIX for Dummies Questions & Answers

Print vertical to horizontal

Hi Masters, I need help to change my vertical data to horisontal input 2015-04-13|JS|741667 2015-04-13|JSJ|2272 2015-04-13|TMS|107099 2015-04-12|JMD|47945 2015-04-13|TM|760024 2015-04-13|JM|484508 2015-04-14|JMJ|318 2015-04-14|JSD|54436 2015-04-13|JM|15410 Output... (2 Replies)
Discussion started by: radius
2 Replies

3. Shell Programming and Scripting

Change the vertical logs to horizontal line

Hi, cat log EPC-SubsId: 3333 EPC-GrIds: RTGHUPA:1:15-11-2013T19:59,22-11-2013T19:59 EPC-GrIds: PrimaXGB_23:10 EPC-SubsId: 4444 EPC-GrIds: RTGHUPB:1:15-11-2013T19:59,22-11-2013T19:59 EPC-SubId: 5555 EPC-GrIds: RTGHUPC:1:15-11-2013T19:59,22-11-2013T19:59 EPC-SubsId: 6666... (1 Reply)
Discussion started by: justbow
1 Replies

4. Shell Programming and Scripting

How do i do the vertical to horizontal??

51009 8746 8912 17986 20315 24998 5368 38934 7805 8566 (4 Replies)
Discussion started by: nikhil jain
4 Replies

5. Shell Programming and Scripting

Vertical And Horizontal Pivoting

Hi All, My Input data is: A=1 B=2 My desired Output should be: A|B 1|2 Thanks in advance... (3 Replies)
Discussion started by: kmsekhar
3 Replies

6. Shell Programming and Scripting

change log vertical to horizontal lines

Hi, Need help unix command to change this : become this Anyone can help me?:wall: (2 Replies)
Discussion started by: justbow
2 Replies

7. UNIX for Dummies Questions & Answers

vertical to horizontal

dear all, i'm new to unix and i try to figure out the best case for making list of vertical text to become horizontal and skip the line 1 and 2. example text : Data DATE XXXXX MAX 47 53 49 51 48 48 7 46 51 8 25 (6 Replies)
Discussion started by: andrisetia
6 Replies

8. UNIX for Dummies Questions & Answers

Horizontal to vertical

Hi, Silly question, if I have an excel file that looks something like this: ................. Subject 1 Subject 2 Subject 3 Subject 4 Fever..............13...........9.............23..........14 Headache.........2............12...........18..........23... (3 Replies)
Discussion started by: Xterra
3 Replies

9. Shell Programming and Scripting

combine files in horizontal way, not vertical.

Hi Everyone, I have three files. FileA: aaaa aaaa bb ccc FileB: 21 2 FileC: eeeeeee e eee ee Would like to combine three of them, not like cat, to cat three files, but the output should be like: (3 Replies)
Discussion started by: jimmy_y
3 Replies

10. Shell Programming and Scripting

Vertical an horizontal pivoing in unix

Please help me to do Vertical an horizontal pivoing in unix in single run. The input file is like this- MRKT|PROD|PRD|FACT1|FACT2|FACT3|FACT4 M1|P1|PR1|F11|F12|F13|F14 M1|P1|PR2|F21|F22|F23|F24 M1|P1|PR3|F31|F32|F33|F34 M2|P2|PR1|F41|F42|F43|F44 M2|P2|PR2|F51|F53|F54|F55... (4 Replies)
Discussion started by: marut_ashu
4 Replies
Login or Register to Ask a Question