Remove & insertion of data in a same file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove & insertion of data in a same file
# 8  
Old 03-01-2007
Code:
awk -F"," -v file="$filenm" '
{
print > file;
 str=$1 ; str_2nd_fld = $2;
 while ( getline )
 {
        while( str_2nd_fld != $1 &&  str == $1 )
        { str1 = str1 "\n" $0 ; str_2nd_fld = $2; getline }
	if ( str_2nd_fld == $1 ) { n=split(str1,arr,"\n"); print arr[n] > file; sub("\n[^\n]*$","",str1) }
        txt = str1 txt
        str1=""
        if( $0 != "" ) print > file;
        str=$1 ;
        str_2nd_fld = $2;
 }
}
END { sub("^ *\n","",txt );print txt > file} ' < $filenm

# 9  
Old 03-01-2007
This script is eating up the very first record of the expected output.

Output I received as -
B,F
B,E
E,G
E,H
A,C
A,D

record A,B got omitted. How can it be done?

Also, I will be thankful, if you can point me to the site where I can have a decent start to learn inside programming of the awk.
# 10  
Old 03-01-2007
I dont see any problems
Code:
$ cat file
A,B
A,C
A,D
B,F
B,E
E,G
E,H
$ filenm=file
$ awk -F"," -v file="$filenm" '
> {
> print > file;
>  str=$1 ; str_2nd_fld = $2;
>  while ( getline )
>  {
>        while( str_2nd_fld != $1 &&  str == $1 )
>        { str1 = str1 "\n" $0 ; str_2nd_fld = $2; getline }
>         while( str_2nd_fld != $1 &&  str == $1 )
>         { str1 = str1 "\n" $0 ; str_2nd_fld = $2; getline }
>       if ( str_2nd_fld == $1 ) { n=split(str1,arr,"\n"); print arr[n] > file; sub("\n[^\n]*$","",str1) }
>         txt = str1 txt
>         str1=""
>         if( $0 != "" ) print > file;
>         str=$1 ;
>         str_2nd_fld = $2;
>  }
> }
> END { sub("^ *\n","",txt );print txt > file} ' < file
$ cat file
A,B
B,F
B,E
E,G
E,H
A,C
A,D

check this post for awk tutorial
https://www.unix.com/showthread.php?p...#post302108502
# 11  
Old 03-01-2007
You were right, I was missing the very first "print > file;" line.

Your link helped me to locate that. Thanks a lot.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

To remove any invisible and special characters from the file(exclude @!#$&*)

Hi Guys, My requirement is to remove any invisible and special characters from the file like control M(carriage return) and alt numerics and it should not replace @#!$% abc|xyz|acd¥£ó adc|123| 12áí Please help on this. Thanks Rakesh (1 Reply)
Discussion started by: rakeshp
1 Replies

2. Shell Programming and Scripting

Remove data from file

Hi I have file with filename as below and need to remove based on date in it. Data in file: filename_092013.csv filename_082013.csv filename_072013.csv I want to remove filenames which are generated 15 days back. where date is generated in file. I need to create another file with... (8 Replies)
Discussion started by: cnrj
8 Replies

3. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

4. UNIX for Dummies Questions & Answers

Remove part of file name with sed & mv

Ok, so I have bunch of files that are named "orange__file_name.asm" and I want to batch rename them to "file_name.asm" I know that using "ls | sed s/orange__//" will get rid of the part of the file name I do not want. But how do I combine that with the mv command to actually do it? Thanks JG (5 Replies)
Discussion started by: john galt
5 Replies

5. Shell Programming and Scripting

Remove 5th & 6th <td> from file

I have a page with a 6<td> structure. I need to strip the 5th and 6th <td></td> from the structure. The data in the 4th <td> varies, I can't get a pattern on it. So, I tried setting up a count of the td occurrence and then remove the 5th and 6th, but I'm not making any progress. Is there an easy,... (6 Replies)
Discussion started by: dba_frog
6 Replies

6. Shell Programming and Scripting

Remove 5th & 6th <td> from file

I have a page with a 6<td> structure. I need to strip the 5th and 6th <td></td> from the structure. The data in the 4th <td> varies, so I can't get a pattern on it. So, I tried setting up a count of the td occurrence and then remove the 5th and 6th, but I'm not making an progress. Is there an... (0 Replies)
Discussion started by: dba_frog
0 Replies

7. Shell Programming and Scripting

Extract data from an XML file & write into a CSV file

Hi All, I am having an XML tag like: <detail sim_ser_no_1="898407109001000090" imsi_1="452070001000090"> <security>ADM1=????</security> <security>PIN1=????</security> <security>PIN2=????</security> ... (2 Replies)
Discussion started by: ss_ss
2 Replies

8. Shell Programming and Scripting

Insertion in a file

Hi All, I want to insert a line just befor the lst line in a file. please can anyone advise. Cheers, Shazin (3 Replies)
Discussion started by: Shazin
3 Replies

9. UNIX for Dummies Questions & Answers

Search for & edit rows & columns in data file and pipe

Dear unix gurus, I have a data file with header information about a subject and also 3 columns of n rows of data on various items he owns. The data file looks something like this: adam peter blah blah blah blah blah blah car 01 30 200 02 31 400 03 57 121 .. .. .. .. .. .. n y... (8 Replies)
Discussion started by: tintin72
8 Replies

10. Shell Programming and Scripting

Remove Headers throughout a data file

I have a data file with over 500,000 records/lines that has the header throughout the file. SEQ_ID Name Start_Date Ins_date Add1 Add2 1 Harris 04/02/08 03/02/08 333 Main Suite 101 2 Smith 02/03/08 01/23/08 287 Jenkins SEQ_ID Name ... (3 Replies)
Discussion started by: psmall
3 Replies
Login or Register to Ask a Question