Exclude the header row while splitting the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exclude the header row while splitting the file
# 1  
Old 07-18-2013
Exclude the header row while splitting the file

Hi All,
i have script like ...

Code:
 "TYPE_ID" "ID"    "LIST_ID"
"18"    "52010" "1059"
"18"    "52010" "1059"
"18"    "52010" "1059"
"18"    "52010" "1059"

i am using the below code it's not taking the header row.

Code:
 awk -F"\t" -v file=test1.txt -v file1=test2.txt ' {
    if( ($3=="\"1010\"") || ($3=="\"17003\"") ) { print $0 >> file }
     else
    { if( ($3=="\"1059\"") || ($3=="\"14958\"") ) { print $0 >file1 } } } ' test.txt

required output should be with header row if any file creation. My code not taking the header row.

Thanks,
# 2  
Old 07-18-2013
Code:
 awk -F"\t" -v file=test1.txt -v file1=test2.txt ' { if(NR==1){print;next} else
    if( ($3=="\"1010\"") || ($3=="\"17003\"") ) { print $0 >> file }
     else
    { if( ($3=="\"1059\"") || ($3=="\"14958\"") ) { print $0 >file1 } } } ' test.txt

# 3  
Old 07-18-2013
Thanks ...
if i use this data not coming header row in the two files.
Code:
"TYPE_ID"       "ID"    "LIST_ID"
"18"    "52010" "1059"
"18"    "52010" "1010"
"18"    "52010" "1059"
"18"    "52010" "1059"

# 4  
Old 07-18-2013
Code:
awk -F"\t" -v file=test1.txt -v file1=test2.txt ' { if(NR==1){print $0>file;print $0>file1;next} else
    if( ($3=="\"1010\"") || ($3=="\"17003\"") ) { print $0 >> file }
     else
    { if( ($3=="\"1059\"") || ($3=="\"14958\"") ) { print $0 >file1 } } } ' test.txt

# 5  
Old 07-18-2013
Thanks,if we use below data test1.txt with header created. but test2.txt only creating the header without data this one i don't want because there no data for this one.

Code:
 "TYPE_ID"       "ID"    "LIST_ID"
"18"    "52010" "1010"
"18"    "52010" "1010"
"18"    "52010" "1010"
"18"    "52010" "1010"

# 6  
Old 07-19-2013
Try this. This code uses hardcoded header values in case data exists for the particular criteria.

Code:
awk  -v file=test1.txt -v file1=test2.txt  '
    {if( ($3=="\"1010\"") || ($3=="\"17003\"") ) 
	{if (H==0) {print "\"TYPE_ID\"\t\"ID\"\t\"LIST_ID\"" >file; print $0 > file; H=1} else { print $0 > file} }
     else
      { if( ($3=="\"1059\"") || ($3=="\"14958\"") ) 
	{ if (H1==0) {print "\"TYPE_ID\"\t\"ID\"\t\"LIST_ID\"" >file1; print $0 > file1; H1=1} else { print $0 > file1} }
      } 
    } ' test.txt

# 7  
Old 07-19-2013
Using the header from the 1st line of the input file (after changing the sample input by replacing all sequences of spaces with a tab), the following seems to do what you want:
Code:
awk -F"\t" -v file=test1.txt -v file1=test2.txt '
NR == 1 {
        H = $0
        next
}
{       if($3 == "\"1010\"" || $3 == "\"17003\"") {
                if(! HC++) print H > file
                print > file
        } else  if($3 == "\"1059\"" || $3 == "\"14958\"") {
                if(! H1C++) print H > file1
                print > file1
        }
}' test.txt

only producing test2.txt with the sample input in test.txt shown in the 1st message in this thread.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

2. Shell Programming and Scripting

How to display the header of a matched row in a file?

Hi, So I am trying to print the first row(header) first column alongwith the matched value. But I am not sure how do I print the same, by matching a pattern located in the file eg File contents Name Place Jim NY Jill NJ Cathy CA Sam TX Daniel FL And what I want is... (2 Replies)
Discussion started by: sidnow
2 Replies

3. UNIX for Dummies Questions & Answers

File Row Line Count without Header Footer

Hi There! I am saving the file count of all files in a directory to an output file using: wc -l * > FileCount.txt I get: 114 G4SXORD 3 G4SXORH 0 G4SXORP 117 total But this count includes header and footer. I want to subtract 2 from the count and get ... (7 Replies)
Discussion started by: gagan8877
7 Replies

4. Shell Programming and Scripting

Add column header and row header

Hi, I have an input like this 1 2 3 4 2 3 4 5 4 5 6 7 I would like to count the no. of columns and print a header with a prefix "Col". I would also like to count the no. of rows and print as first column with each line number with a prefix "Row" So, my output would be ... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

Test command:Duplicate Header row in Log File

I have a script that is inventorying (not sure if thats a word) my environment. It goes out and pulls Hostname OS, IP, and ENV (dev, prod, etc)..then spits all that to a logfile At the top of my script i have a check to see if the logfile exist. ] || touch $LOGFILE && echo "ENV" "\t"... (3 Replies)
Discussion started by: nitrobass24
3 Replies

6. Shell Programming and Scripting

Exclude the header row in the file to validate

Hi All, File contains header row.. we need to exclude the header row...no need to validate the first row in the file. Data in the file should take valid data(two columns)..we need to exclude the more than two columns in the file except the first line. email|firstname a|123|100 b|345... (4 Replies)
Discussion started by: bmk
4 Replies

7. UNIX for Dummies Questions & Answers

append column and row header to a file in awk script.

Hi! Is there a way to append column and row header to a file in awk script. For example if I have Jane F 39 manager Carlos M 40 system administrator Sam F 20 programmer and I want it to be # name gend age occup 1 Jane F 39 manager 2 Carlos M ... (4 Replies)
Discussion started by: FUTURE_EINSTEIN
4 Replies

8. Shell Programming and Scripting

Adding header to sub files after splitting the main file using AWK

Hi Folks, I have a file like: mainfile.txt: ------------- file1 abc def xyz file1 aaa pqr xyz file2 lmn ghi xyz file2 bbb tuv xyz I need output having two files file1 and file2. file1: ------ Name State Country abc def xyz aaa pqr xyz file2: (3 Replies)
Discussion started by: tanmay.gemini
3 Replies

9. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

10. Shell Programming and Scripting

how to exclude the header in shell script using awk

Hello Everyone In my shell script, I am retrieving the cluster ID and node number of an LPAR using the following command - lsclcfg -l This command's output looks as follows - CLUSTER_NAME CLUSTER_ID NODE_NR sch1h004 6104567 3 I want to store only the... (3 Replies)
Discussion started by: gates1580
3 Replies
Login or Register to Ask a Question