columns manipulation in different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting columns manipulation in different files
# 1  
Old 08-04-2010
columns manipulation in different files

Hi all,

I have 3 files with data as :

Code:
1.txt
1
1
1
5

Code:
2.txt
-5
1
0
3

Code:
3.txt
0
1
0
1

Based on 3.txt with values as 0 and 1 only, I want o/p as if 3.txt is 0 then element of 1.txt should print else element of 2.txt should print.

i.e;
Code:
1
1
1
3

I have written something wrong as it only checks for 1st element and not till end of 3.txt

Code:
paste 3.txt 1.txt 2.txt | awk '{if ($1 = "0") {print $2 > "opt"} else {print $3 > "opt"}} '

Please help.

Thanks
# 2  
Old 08-04-2010
Two things that need to be changed:

Invoke awk with the -F '\t' option to set the field delimiter to tabs as paste separates fields with a tab.

Secondly, your test in the if needs to be a double equal (==) rather than a single which is always resulting in true.

Similar to your programme:
Code:
paste f3 f1 f2 | awk -F '\t' '
{
        if ($1  ==  "0")
                print $2 ;
        else
                print $3;
}'

This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

2. Shell Programming and Scripting

Adding columns from 2 files with variable number of columns

I have two files, file1 and file2 who have identical number of rows and columns. However, the script is supposed to be used for for different files and I cannot know the format in advance. Also, the number of columns changes within the file, some rows have more and some less columns (they are... (13 Replies)
Discussion started by: maya3
13 Replies

3. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

4. Shell Programming and Scripting

[Solved] awk manipulation of sequentially named files

Hello, I am a very novice user of awk, I have a set of files named file001, file002, file003, file004, etc., each contains four fields (columns of data) separated each by a uneven number of spaces. I want to substitute those spaces by a TAB, so I am using this line of awk script: awk -v OFS="\t"... (4 Replies)
Discussion started by: jaldo0805
4 Replies

5. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

6. Shell Programming and Scripting

Files manipulation in Linux

Hello , I started learning shell scripting recently and I was able to use grep and awk command to get a columns or rows with a certain format out of one file and put them in a new one using : grep -e "N -t 99.998" xxx.txt > temp1.txt and awk < temp1.txt '{print $5," ",$ 7} '> temp32.txt... (3 Replies)
Discussion started by: lolypop
3 Replies

7. Shell Programming and Scripting

Bash folder manipulation - selecting/copying specified files

Bash/scripting newbie here - I feel this might be a trivial problem, but I'm not sure how to tackle it. I've got a folder of a year's worth of files, with some random number of files generated every day of the year (but at least one per day). I'm writing a script to automatically grab the file with... (6 Replies)
Discussion started by: WildGooseChased
6 Replies

8. UNIX for Dummies Questions & Answers

Date Manipulation with files

Hi, I have a timestamp stored in a variable start_time=17-JUL-2009 03:45, I need to search a file for this time stamp line by line (where each line has a time stamp in the file) if the time stamp is greater than the start_time variable value need to grep that line for a string and if the string... (2 Replies)
Discussion started by: happyrain
2 Replies

9. UNIX for Advanced & Expert Users

Huge files manipulation

Hi , i need a fast way to delete duplicates entrys from very huge files ( >2 Gbs ) , these files are in plain text. I tried all the usual methods ( awk / sort /uniq / sed /grep .. ) but it always ended with the same result (memory core dump) In using HP-UX large servers. Any advice will... (8 Replies)
Discussion started by: Klashxx
8 Replies

10. Shell Programming and Scripting

Files manipulation with time comparison

Hi guys - I am new to Unix and learning some basics. I have to create a report of files that are in a specific directory and I have to list filenames with specific titles. This report will be created everyday early in the morning, say at 05:00 AM. (see output file format below) The 2 categories... (2 Replies)
Discussion started by: sksahu
2 Replies
Login or Register to Ask a Question