cut and paste columns using awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers cut and paste columns using awk
# 1  
Old 05-08-2009
cut and paste columns using awk

Hi,

Let's say that I have a file called table, I know that if I need to see a the second column for exampls I use:
awk ' {print $2}' table.txt

Is there anyway to use awk to actually cut a column and put it somewhere else in the table?Smilie
# 2  
Old 05-08-2009
For example:

Code:
$ awk '{ print $1" "$3" "$4" "$2 }' table.txt > outputfile.txt

Would move column 2 to the last column.
# 3  
Old 05-08-2009
Code:
awk '{for (i=NF; i>0; i--) { printf("%s ", $i}; print "" }' mycolumnarfile > newfile

reverses columns example: 4 .. 3 .. 2 .. 1
# 4  
Old 05-08-2009
Thanks for replying, but when I do that I see the results in Unix, how can I do that in my original table.txt file?
# 5  
Old 05-08-2009
Quote:
Originally Posted by TonyFullerMalv
For example:

Code:
$ awk '{ print $1" "$3" "$4" "$2 }' table.txt > outputfile.txt

Would move column 2 to the last column.
Quote:
Originally Posted by jim mcnamara
Code:
awk '{for (i=NF; i>0; i--) { printf("%s ", $i}; print "" }' mycolumnarfile > newfile

reverses columns example: 4 .. 3 .. 2 .. 1
Thanks for replying, but when I do that I see the results in Unix, how can I do that in my original table.txt file?
# 6  
Old 05-08-2009
Code:
[awk command here]   table.txt > anothertable.txt
mv anothertable.txt table.txt

# 7  
Old 05-08-2009
Code:
{ rm table.txt; awk '{for (i=NF; i>0; i--) printf("%s%c", $i, (i==1)?RS:OFS)}'  > table.txt; } < table.txt

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk command to paste 1st columns of 2 files.

I have 2 files contains more than 5000 lines, I want to paste the 1st column of both file in an output file using awk. Please suggest to me I had tried paste command but it merges my both column. --- Post updated at 10:26 PM --- file1.txt 2020-01-07 235400 2020-01-07 235400 2020-01-07... (4 Replies)
Discussion started by: Sagar Singh
4 Replies

2. Shell Programming and Scripting

AWK command to cut the desired header columns

Hi Friends, I have a file1 i want to retrieve only the fields which have DEP,CITY,TRANS as headers in other file. Output: I want to give the input as DEP,CITY,TRANS column names to get the output. i used cut command .. but if i have 300 fileds it is more difficult to... (4 Replies)
Discussion started by: i150371485
4 Replies

3. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

4. Shell Programming and Scripting

cut and paste

Hi, Need a help with shell script. I have to search for a string in one of the file, if match found, copy the line to a new file and delete the line from the exisiting file. eg: 83510000000000063800000.1800000.1600000.1600000.2400000.1800000.2000000.21... (6 Replies)
Discussion started by: gpaulose
6 Replies

5. Shell Programming and Scripting

cut and paste?

hi, I have a file with content like this for an employee: EmployeeID 101 Day_type, day vacation,1/2/2009 sick day, 3/2/2009 personal day, 4/5/2009 jury duty day, 5/5/2009 how do I make the result to show: EmployeeID,Day_type,day 101,vacation,1/2/2009 101,sick day,... (6 Replies)
Discussion started by: jbchen
6 Replies

6. Shell Programming and Scripting

cut and paste using awk

Hi i need a favour i have a file which has some trillions of records. The file is like this 11111000000000192831840914000000000000000000000000000 45789899090000000000000000011111111111111111111111111 I want to cut specific postions in each line like cut1-3 and assisgn it to a variable and... (5 Replies)
Discussion started by: richa2.m
5 Replies

7. Shell Programming and Scripting

appending several columns with awk and paste

Hello, I am trying to solve for a couple of hours now the following problem: I have n files and would like to add the third column of each file to a new file: temp1.txt 1 2 3 1 2 3 1 2 3 temp2.txt 1 2 4 1 2 4 1 2 4 1 2 4 temp3.txt (2 Replies)
Discussion started by: creamcheese
2 Replies

8. UNIX for Dummies Questions & Answers

cut, copy + paste

Hi all! How do I cut, copy and paste under unix??? (2 Replies)
Discussion started by: aitor314
2 Replies

9. Shell Programming and Scripting

cut & paste

hi i am new to shell scripting, i have been trying to cut columns numbered 1,4 of a file consisiting of 4 columns. Each column is seperated by 2 spaces. for example: john 6102097199 tennessee usa michel 6734590899 texas USA now, i need to cut the name... (3 Replies)
Discussion started by: t_harsha18
3 Replies

10. UNIX Desktop Questions & Answers

Cut, Copy and Paste with X

One of the things that I have learned to take for granted in the Win32 world is the cut, copy and paste hotkeys of ^X, ^C and ^V. I use these keys all the time under Win32 to copy and paste information from one GUI into another GUI. My question is, does X have a similiar standard? ... (4 Replies)
Discussion started by: auswipe
4 Replies
Login or Register to Ask a Question