Cutting a column and pasting its number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cutting a column and pasting its number
# 1  
Old 11-12-2014
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 number of elements and a line identifier as "prefix". For example, s_04 contains:
Code:
4 01 A B D F
4 05 C E 1 xx
(etc.)

What I want is to cut each field in turn, and keep track of which field was cut. For example s_04.cut would contain
Code:
4 01 (-1) B D F
4 05 (-1) E 1 xx
4 01 (-2) A D F
4 05 (-2) C 1 xx
4 01 (-3) A B F
4 05 (-3) C E xx
4 01 (-4) A B D
4 05 (-4) C E 1

So far, I'm able to cut each field in turn and append it to the desired output file:
Code:
    for i in $(seq 1 $nwords)
    do
         j=$(( i+2 ))
        # remove ith field, append to *.cut
        cut -d' ' -f"$j" --comp ./out/s_"$nwords" >> ./out/s_"$nwords".cut

The problem is I don't know how to construct a pipe in which somehow $j is echoed/pasted/inserted after the first two fields in the output file. I'm using an awk script to generate the cut fields externally and then using cut and paste to insert it. It works but it's clunky and am looking for something more elegant and "unixy"

I would be grateful for any guidance. In case it matters, I'm working in some recent flavor of Ubuntu.

Alejandro
# 2  
Old 11-12-2014
Does the output order matter? Try
Code:
awk     '       {for (i=3; i<=NF; i++)  {printf "%s", $1 FS $2 FS "(-"i-2")"
                                         for (j=3; j<=NF; j++) if (i!=j) printf " %s", $j
                                         printf "\n"
                                        }
                }
        ' file
4 01 (-1) B D F
4 01 (-2) A D F
4 01 (-3) A B F
4 01 (-4) A B D
4 05 (-1) E 1 xx
4 05 (-2) C 1 xx
4 05 (-3) C E xx
4 05 (-4) C E 1

This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-12-2014
Wow, that's nice, RudiC. Order doesn't matter (actually it does, but I was going to sort output in the next step anyway.) Thank you!
# 4  
Old 11-12-2014
Another approach, but more as an exercise because it lacks clarity:
Code:
awk '{p=$0; n=NF; for(i=3; i<=n ;i++) {$0=p; $i=x; $0=$0; $2=$2 " (-" i-2 ")"; print}}' file | sort -k3,3 -k1,2


Last edited by Scrutinizer; 11-12-2014 at 05:12 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 11-12-2014
Thanks, Scrutinizer. It is an appealing one-liner, but it comes at the cost of clarity (at least to my novice eyes). I ended up saving RudiC's code as a stand-alone awk script and then piping its output to sort. Even though this is a "one-off" project and I don't foresee revisiting it again, in a few months it will be nice to be understand the "magic". ;-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Pasting files with different number of lines

Hi all, I tried to use the paste command to paste two files with different number of lines. e.g. file1 A 1 B 1 C 2 D 2 file2 A 2 B 3 C 4 D 4 E 4 (2 Replies)
Discussion started by: f_o_555
2 Replies

3. UNIX for Dummies Questions & Answers

[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... (9 Replies)
Discussion started by: mcampos7
9 Replies

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. Shell Programming and Scripting

Cutting number from range in xml file

Hi folks, I need to find the following value: First,I need to find the starting section by finding the line: <process-type id="OC4J_RiGHTv_${SCHEMA_NAME}" module-id="OC4J"> Second,under this line I need to find the following line: <port id="rmi" range="3765-3776"/> And third,from this line... (4 Replies)
Discussion started by: nir_s
4 Replies
Login or Register to Ask a Question