Truncating FILE data BASED ON A PATTERN


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Truncating FILE data BASED ON A PATTERN
# 1  
Old 10-14-2008
Truncating FILE data BASED ON A PATTERN

HI I HAVE A PROBLEM,MY SOURCE FILE IS OF PATTERN
S1,E-Certified,29,29,2.7,Certified,4,3,2.7,,0,0,0
S2,Certified,4,3,2.7,,0,0,0,,0
S3,E-Certified,29,29,2.7,,0,0,0
S4,,0,0,0,,0,0,0,,0,0,0,,0,0,0

AND THE EXPECTED OUTPUT IS

S1,E-Certified,29,29,2.7
S1,Certified,4,3,2.7
S2,Certified,4,3,2.7
S3,E-Certified,29,29,2.7

THE NUMBER OF FIELDS(NF) IN SOURCE FILE MAY BE MORE THAN 500. WHEREEVER THE FIRST NULL VALUE COMES IT SHOULD TRUNCATE THE ROW.
I TRIED TO SOLVE N WROTE THIS CODE,
Code:
awk -F\, '{
        if ($2!="") printf$1FS fi;c=0
        for(i=2;i<=NF;i++)
        {
        if ($i=="") i=NF+1 fi;
        printf$i ($i!=""?++c%4?i==NF?RS:FS:(i<NF?RS$1FS:RS):EXIT);
}
        }'

bUT THE OUTPUT COMING IS
S1,E-Certified,29,29,2.7
S1,Certified,4,3,2.7
S1,S2,Certified,4,3,2.7
S2,S3,E-Certified,29,29,2.7
S3,
I AM NOT ABLE TO FIND OUT THE ERROR. IF ANYONE FINDS ANY PROBABLE SOLUTION PLZ SUGGEST ME.

I Solved the problem. The code should be

awk -F\, '{
if ($2!="") printf$1FS fi;c=0
for(i=2;i<=NF;i++)
{
if ($i=="") i+=NF fi;
printf$i ($i!=""?++c%4?i==NF?RS:FSSmilie$(i+1)!=""?RS$1FS:RS):EXIT);
}
}'

Last edited by pkumar3; 10-15-2008 at 01:49 AM.. Reason: added code tags
# 2  
Old 10-14-2008
What's going on when field 2 is blank?
If I understand you correctly, you want:
Code:
awk  'BEGIN { FS=OFS="," } !length($2) { next; } {
   for (i=3;i<=NF;++i) {
      if ($i=="") { NF=i-1; break;}
   }
   print $0;
}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. Shell Programming and Scripting

Copy data to new file based on input pattern

Hi All, I want to create a new file based on certain conditions and copy only those conditioned data to new file. Input Data is as it looks below. ORDER|Header|Add|32|32|1616 ORDER|Details1......... ORDER|Details2......... ORDER|Details3......... ORDER|Details4............ (10 Replies)
Discussion started by: grvk101
10 Replies

3. UNIX for Beginners Questions & Answers

Extract file name based on the pattern

Hello All, I have multiple files in a hadoop /tmp/cloudera directory. Filename are as follows ABC_DATA_BAD5A_RO_F_20161104.CSV ABC_DATA_BAD6C_VR_F_20161202.CSV ABC_DATA_BAD7A_TR_F_20162104.CSV ABC_DATA_BAD2A_BR_F_20161803.CSV ABC_DATA_BAD3T_KT_F_20160106.CSV I just need filenames... (6 Replies)
Discussion started by: prajaktaraut
6 Replies

4. Shell Programming and Scripting

Insert data based on pattern

if it is finding some data based on pattern 'test' then insert else if has no data based on the pattern 'test' then exit successfully cat file | grep test > file2 (3 Replies)
Discussion started by: jagu
3 Replies

5. Shell Programming and Scripting

Splitting textfile based on pattern and name new file after pattern

Hi there, I am pretty new to those things, so I couldn't figure out how to solve this, and if it is actually that easy. just found that awk could help:(. so i have a textfile with strings and numbers (originally copy pasted from word, therefore some empty cells) in the following structure: SC... (9 Replies)
Discussion started by: luja
9 Replies

6. Shell Programming and Scripting

Generate tabular data based on a column value from an existing data file

Hi, I have a data file with : 01/28/2012,1,1,98995 01/28/2012,1,2,7195 01/29/2012,1,1,98995 01/29/2012,1,2,7195 01/30/2012,1,1,98896 01/30/2012,1,2,7083 01/31/2012,1,1,98896 01/31/2012,1,2,7083 02/01/2012,1,1,98896 02/01/2012,1,2,7083 02/02/2012,1,1,98899 02/02/2012,1,2,7083 I... (1 Reply)
Discussion started by: himanish
1 Replies

7. Shell Programming and Scripting

how to get data from hex file using SED or AWK based on pattern sign

I have a binary (hex) file I need to parse to get some data which are encoded this way: .* b4 . . . 01 12 .* af .* 83 L1 x1 x2 xL 84 L2 y1 y2 yL By another words there is a stream of hexadecimal bytes (in my example separated by space for better readability). I need to get value stored in... (3 Replies)
Discussion started by: sameucho
3 Replies

8. UNIX for Dummies Questions & Answers

Truncating file based on date

Hi, I need to truncate a file based on date.Suppose i have a log file which is getting updated every date,i need to keep 7 days worth of data(like sysdate-7) and rest i want to truncate it.Can some help me? (5 Replies)
Discussion started by: Param0073
5 Replies

9. Shell Programming and Scripting

Merge two file data together based on specific pattern match

My input: File_1: 2000_t g1110.b1 abb.1 2001_t g1111.b1 abb.2 abb.2 g1112.b1 abb.3 2002_t . . File_2: 2000_t Ali england 135 abb.1 Zoe british 150 2001_t Ali england 305 g1111.b1 Lucy russia 126 (6 Replies)
Discussion started by: patrick87
6 Replies

10. UNIX for Dummies Questions & Answers

how to change a particular value in a file based on a pattern

how to change a particular value in a line of a file based on a pattern eg: source file will be like ================= 99999999999 maximum number(0) ksuisikjjsl;;l skjss''llsl minimum number 0000000 maximum number of golds(0) target should be like ================= ... (9 Replies)
Discussion started by: orbeyen
9 Replies
Login or Register to Ask a Question