[Q] cutting from one column and pasting to another


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Q] cutting from one column and pasting to another
# 1  
Old 10-31-2011
[Q] cutting from one column and pasting to another

I have a file laid out in columns with the first two lines line being:
219 432 4567
219 432 4587

I need to create a single line command to cut the characters in the 5th column and paste them back to the first column in the same file. (Hint:Two good solutions exist, one in which you use a semicolon and one with more finesse in which you use a pipe character)


I was thinking something like: cut -c5 file | paste - file


but the output doesn't look right:
4 219 432 4567
4 219 432 4587

Help appreciated!!
# 2  
Old 10-31-2011
I hope this is what you are looking for:


#
Code:
cut -c5 file|paste -d " " - file|awk '{print $1$2,$3,$4}'

Output from above:

Code:
4219 432 4567
4219 432 4587

# 3  
Old 10-31-2011
Code:
sed 's/\(^....\)\(.\)\(.*\)/\2 \1\2\3/' Inp_File

# 4  
Old 10-31-2011
Quote:
Originally Posted by dude2cool
I hope this is what you are looking for:


#
Code:
cut -c5 file|paste -d " " - file|awk '{print $1$2,$3,$4}'

Output from above:

Code:
4219 432 4567
4219 432 4587

Well you answered my question yes, but it looks like I asked the wrong question. I only gave you the first few columns when in actuality the first two lines are:
Code:
219 432 4567 Harrison    Joel      M 4540 Accountant      09-12-1985
219 432 4587 Mitchell    Barbera   C 4541 Admin Asst      12-14-1995

Sorry about that, this is an intro class, so I don't know too much..
# 5  
Old 10-31-2011
Quote:
Originally Posted by mcampos7
Sorry about that, this is an intro class, so I don't know too much..
If you have homework questions, please post it here:
Senior Advisor - https://www.unix.com
# 6  
Old 10-31-2011
Here you go,

#
Code:
sed 's/\(...\)\(..\)\(.*\)/\2\1\2\3/' <path to your file>

Output from above:

4219 432 4567 Harrison Joel M 4540 Accountant 09-12-1985
4219 432 4587 Mitchell Barbera C 4541 Admin Asst 12-14-1995

@Shell_Life, Your original code above was not working, I just modified it to above.
# 7  
Old 10-31-2011
Quote:
Originally Posted by Shell_Life
If you have homework questions, please post it here:
I already received the 2pts this question was worth for coming up with:
Code:
cut -c5 file | paste - file

For my own curiosity I'd like to know the full answer...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cutting a column and pasting its number

Hello Gurus, This is my first ever post here. I tried looking for similar material but came up empty handed. Apologies if this is too verbose or if I'm not using the correct formatting. I have files containing a fixed number of elements per line; separator is a single space. Each line has the... (4 Replies)
Discussion started by: stdroid
4 Replies

2. UNIX for Dummies Questions & Answers

Cutting only the 1st column data

Hi All, I having a data file which consists of 20cr records in it. The 1st column is year field which consist of year in format 200809 and fields are seperated with ^. How do i trim it to 2008 in file and save it in a quick time as there are many records so that i can use the file for loading... (3 Replies)
Discussion started by: abhi_123
3 Replies

3. Shell Programming and Scripting

pasting problem

Hi Friends, Please help. I have 2 pipe delimited files; each one has different number of rows in it. I want to use "paste" to concatenate these files. However, while doing it, due to difference in number of rows in each files resulting file is in haphazard manner. E.g. file 1 Reliance Info|... (1 Reply)
Discussion started by: anushree.a
1 Replies

4. Shell Programming and Scripting

help with pasting files in filesystem

quick question.. say i have few files in D or E drive.. i want to paste them in Filesystem that is /home/vivek folder... but when i try to do that it shows some error saying "There is not enough space on the destination. Try to remove files to make space." but i think its due to authorization... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. UNIX for Dummies Questions & Answers

Pasting a column into a text file

I have two text files: One is a single column of numbers and the other is a space delimited text file with multiple columns. I want to paste the single column of numbers into the second column of the latter text file. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

Any shell scripts for cutting and pasting part of data?

Hi, I have a tab-delimited txt file as below. It is part of the original file. I want to cut the lines starting with "3" in column1 and paste them before the lines starting with "1" in column 1. So I will get Anyone knows any simple shell scripts to do that? The original file is... (5 Replies)
Discussion started by: cliffyiu
5 Replies

7. UNIX for Advanced & Expert Users

Bash shell: Cutting pasting only parts of the name of a directory into a variable

I have a script in a directory and want to search the directory before like follows: i=0 for file in ../HN_* do echo $file ((i+=1)) echo $i done Currently I get following output: ../HN_2 1 ../HN_3 2 (2 Replies)
Discussion started by: ABE2202
2 Replies

8. Shell Programming and Scripting

How to cut first line only from a text near a specific column without cutting a word

First I have to say thank you to this community and this forum. You helped me very much builing several useful scripts. Now, I can't get a solution the following problem, I'm stuck somehow. Maybe someone has an idea. In short, I dump a site via lynx and pipe the output in a file. I need to... (7 Replies)
Discussion started by: lowmaster
7 Replies

9. UNIX for Dummies Questions & Answers

Cutting the first 8 chars of a 2nd column

I am having a stupid moment :-) I have a tab-delimited file with 2 columns. I want to keep the first column as it is, but I only want the first 8 characters of the 2nd column. Example of input file data: --------------------------------- CATERPILLARS CW001651K.dwg... (9 Replies)
Discussion started by: cassj
9 Replies

10. Shell Programming and Scripting

Need help recurrently cutting single column file

Hi there ! I have file with single column. I want to cut that column at fixed number of rows each time and paste in another file, in a way that in new file, the each cutting appear as separate columns. I mean cutting file with one column of 10000 rows, with 100 rows each time, and in new file... (3 Replies)
Discussion started by: jacks
3 Replies
Login or Register to Ask a Question