Search last column of INPUT.txt in TABLEs text and add correspond columns to INPUT.txt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search last column of INPUT.txt in TABLEs text and add correspond columns to INPUT.txt
# 1  
Old 01-27-2018
Search last column of INPUT.txt in TABLEs text and add correspond columns to INPUT.txt

Hi dears
i use bash shell

i have INPUT.txt
like this
number of columns different in one
some row have 12 , some 11 columns

see last column


Code:
  INPUT.txt
 CodeGender Age Grade Dialect Session Sentence Start End Length Phonemic Phonetic
  63 M 27 BS/BA TEHRANI 3 4 298320 310050 11730 j j 
 63 M 27 BS/BA TEHRANI 3 4 310050 311430 1380     a 
 63 M 27 BS/BA TEHRANI 3 4 311430 312080 650 ] ]

and two TABLE text like this.have two columns.
Code:
  TABLE1.txt
 j feat1
 a feat2 
.
 .
 .

Code:
  TABLE2.txt
 j sp1
 a sp2
 .
 .

I want to add two columns to INPUT.txt
search last column of INPUT in TABLEs text and add correspond columns to INPUT
(i think can merge TABLEs like j feat1 sp1)
and output like this


Code:
  output.txt
 CodeGender Age Grade Dialect Session Sentence Start End Length Phonemic Phonetic feat sp
 63 M 27 BS/BA TEHRANI 3 4 298320 310050 11730 j j feat1 sp1
 63 M 27 BS/BA TEHRANI 3 4 310050 311430 1380       a feat2 sp2
 63 M 27 BS/BA TEHRANI 3 4 311430 312080 650 ] ] . . 
.
.

thanks all

Last edited by alii; 01-27-2018 at 09:09 AM..
# 2  
Old 01-27-2018
In your sample INPUT.TXT, the header line has 11 fields as "CODE" and "GENDER" are contiguous which they are not in the data lines.
How do you determine the two new header fields?

Howsoever, try (with file1 .. file3 representing your input / tables)
Code:
awk '
function CKCOM(T, X)    {if (!T) return X
                         for (i=1; i<=length(T) && substr(T, i, 1) == substr(X, i, 1); i++);
                         return substr (T, 1, i-1)
                        }
FNR == 1        {FN++
                 T[$NF] = OFS HD[1] OFS HD[2]
                }

FN < 3          {T[$1] = T[$1] OFS $2
                 HD[FN] = CKCOM(HD[FN], $2)
                 next
                }

                {print $0 T[$NF]
                }
' file[1-3]
CodeGender Age Grade Dialect Session Sentence Start End Length Phonemic Phonetic feat sp
63 M 27 BS/BA TEHRANI 3 4 298320 310050 11730 j j  feat1 sp1
63 M 27 BS/BA TEHRANI 3 4 310050 311430 1380   a  feat2 sp2
63 M 27 BS/BA TEHRANI 3 4 311430 312080 650 ] ]


Last edited by RudiC; 01-27-2018 at 10:15 AM..
# 3  
Old 01-27-2018
Code:
awk '
FILENAME ~ /TABLE/ {w[$1]=w[$1] " " $2; next}
NR > 1 {$0=$0 w[$NF]}
{print $0}
' TABLE1.txt TABLE2.txt INPUT.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk, sed, shell all words in INPUT.txt find in column1 of TABLE.txt and replce with column2 in

Hi dears i have text file like this: INPUT.txt 001_1_173 j nuh ]az 001_1_174 j ]esma. nuh ]/.xori . . . and have another text like this TABLE.txt j j nuh word1... (6 Replies)
Discussion started by: alii
6 Replies

2. Shell Programming and Scripting

How to get the shell script to read the .txt file as an input/data?

i have written my shell script in notepad however i am struggling to pass the data file to be read to the script the data file is of .txt format. My target is to run the shell script from the terminal and pass 3 arguments e.g. polg@DESKTOP-BVPDC5C:~/CS1420/coursework$ bash valsplit.sh input.txt... (11 Replies)
Discussion started by: Gurdza32
11 Replies

3. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

4. Shell Programming and Scripting

Get the input from user and save it as .txt file

Hi friends, I am pretty new to shell scripting, please help me in this Scenario. for example, If I have one file called input.txt once I run the script, 1.It has to delete the old input.txt and create the new input.txt (if old input.txt is not there, no offence, just it has to create a... (2 Replies)
Discussion started by: Padmanabhan
2 Replies

5. Shell Programming and Scripting

Read input txt while find copy

I have a text file called file.txt which has a list of file as shown below that i need to find and copy to a particular location. FILENAMES skter.pdf abcdf.sas tereen.lst abc12.txt i am using following code and it never works however i try it. cat file.txt | while read FILENAME;do find... (4 Replies)
Discussion started by: MILAN KUMAR
4 Replies

6. UNIX for Dummies Questions & Answers

Add a new column to txt file containing filename

I would like help adding a new column to a large txt file (~10MB) that contains the filename. I have searched other posts but have not found an adequate solution. I need this extra column so I can concatenate >100 files and perform awk searches on this large file. My current txt file look... (4 Replies)
Discussion started by: kellywilliams
4 Replies

7. Shell Programming and Scripting

Help awking a 'head -1 file.txt' input

Hi there, my ksh script collects a procstack trace for a particular pid and then greps it by a transaction id to find out the pthread ID: ---------- tid# 1876087 (pthread ID: 4466) ---------- So the pthread ID I want is 4466 in this case, and it is assighed to the variable $pthread.... (4 Replies)
Discussion started by: tmf33uk
4 Replies

8. Shell Programming and Scripting

Input file redirect in output path and want name as inputfilename_new.txt

not required this time (6 Replies)
Discussion started by: Sandeep_Malik
6 Replies

9. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies

10. Shell Programming and Scripting

How to input .txt file into .xls spreadsheet

I need to take the totals from my script and input them into a excel spreadsheet. Right now, I just copy and paste. Is there an easier way? 3906 is the total jobs in ABEND state 4005 is the total jobs in SUCC state 1050 is the total jobs in HOLD state (1 Reply)
Discussion started by: wereyou
1 Replies
Login or Register to Ask a Question