How to swap column?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to swap column?
# 1  
Old 05-18-2012
Data How to swap column?

I am new to Unix. I try to match searchstring in 4 files and display as
file : matchline number : line match with searchstring

My code is
Code:
 sed -n "/$line/ p" $1 | sed -e "a$1" | sed '{ N  s/\n/ / }'

Moderator's Comments:
Mod Comment MODERATOR NOTE-I think that is how you intended the code to read


My result is
matchline number line match with searchstring filename

problem
======
1. I try to put delimeter : but fail.
2. If I try ( sed -e "a$1" |sed -n "/$line/ p" $1), the result is display all lines of the file rather than match line.

I try Awk but got error. If someone know my error please help. Thank you in advance.

Last edited by radoulov; 05-18-2012 at 10:13 AM.. Reason: Please use CodeTags for code and examples
# 2  
Old 05-18-2012
Code:
awk '/pattern/{print FILENAME,FNR,$0}' *

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 05-18-2012
Thank you . But still some problem,

I have 3 fields in "res.out" file

(character field numeric field character field)
------------------------------------------------------
Code:
abc 123 this is my first line
eaw 45 testing line thee

I want to swap first 2 columns and insert delimeter :
Code:
123 : abc : this is my first line
45 : eaw : testing line thee

I use
awk '{ print $2 " " $1 " "}' "res.out"

I do not know how to add delimeter ":" and how to swap column 2 to column 1
Please help.

Thank you

Last edited by joeyg; 05-18-2012 at 03:06 PM.. Reason: CodeTags please
# 4  
Old 05-18-2012
what about something like:

Code:
$ echo abc 123 the brown dog | sed 's/ /~/1;s/ /~/1' | awk -F"~" '{print $2,":",$1,":",$3'}
123 : abc : the brown dog

You can do two substitutions in one sed command; needed that first.
This User Gave Thanks to joeyg For This Post:
# 5  
Old 05-18-2012
Thks.
I use
Code:
$ awk 'BEGIN {OFS=":"}{ print $2,$1,$3}' "res.out"

input
Code:
abc 123 the brown dog

result is
Code:
123 : abc : the

"brown dog" Rest of the line does not appear. Because awk think " " is sepearator.

Pls help

Thks

Last edited by Scrutinizer; 05-19-2012 at 03:00 AM.. Reason: code tags
# 6  
Old 05-18-2012
You printed 3 columns, ergo you get 3 columns.

You can also just put : inside the fields themselves though, and alter individual columns, then print the entire line.

Code:
awk '{T=$1; $1=$2" :"; $2=T " :" } 1' filename

These 2 Users Gave Thanks to Corona688 For This Post:
# 7  
Old 05-19-2012
Question thank you

Thank you very much for all of you. I have been struggling it with sleepless night for 10 days.

Thanks again



Last edited by jat; 05-19-2012 at 05:13 PM.. Reason: code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Explain the output of swap -s and swap -l

Hi Solaris Folks :), I need to calculate the swap usage on solaris server, please let me understand the output of below swap -s and swap -l commands. $swap -s total: 1774912k bytes allocated + 240616k reserved = 2015528k used, 14542512k available $swap -l swapfile dev swaplo... (6 Replies)
Discussion started by: seenuvasan1985
6 Replies

2. Shell Programming and Scripting

How to swap the column to row?

Hi, I am new to shell scripting and have one complex req like below. I have one file like below and want oupt put like the 2nd file. Time|Measures|Product|store1|store2|store3|store4|store5|-- Week Ending 06-15-14|Dollar Sales|BACARDI PLUS|40|30|20|23|45| Week Ending 06-22-14|Unit... (3 Replies)
Discussion started by: jaggy
3 Replies

3. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

Difference of the same column when two other column matches and one column differs less than 1 hour

This is my input file : # cat list 20130430121600, cucm, location,76,2 20130430121600,cucm1,location1,76,4 20130430122000,cucm,location,80,8 20130430122000,cucm1,location1,90,8 20130430140000,cucm1,location1,87,11 20130430140000, cucm,location,67,9 This is the required output ... (1 Reply)
Discussion started by: Lakshmikumari
1 Replies

5. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies

6. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

7. HP-UX

Swap device file and swap sapce

Hi I have an integrity machine rx7620 and rx8640 running hp-ux 11.31. I'm planning to fine tune the system: - I would like to know when does the memory swap space spill over to the device swap space? - And how much % of memory swap utilization should be specified (swap space device... (6 Replies)
Discussion started by: lamoul
6 Replies

8. Red Hat

swap not defined as swap

free -m : 1023 total swap space created default partition /dev/sdb1 50M using fdisk. i did write the changes. #mkswap /dev/sdb1 #swapon /dev/sdb1 free -m : 1078 total swap space this shows that the swap is on Question : i did not change the type LINUX SWAP (82) in fdisk. so why is... (5 Replies)
Discussion started by: dplinux
5 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. Solaris

Swap config - Mirror swap or not?

Hello and thanks in advance. I have a Sun box with raid 1 on the O/S disks using solaris svm. I want to unmirror my swap partition, and add the slice on the second disk as an additional swap device. This would give me twice as much swap space. I have been warned not to do this by some... (3 Replies)
Discussion started by: BG_JrAdmin
3 Replies
Login or Register to Ask a Question