Select some lines from a txt file and create a new file with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Select some lines from a txt file and create a new file with awk
# 1  
Old 09-11-2009
Select some lines from a txt file and create a new file with awk

Hi there, I have a text file with several colums separated by "|;#" I need to search the file extracting all columns starting with the value of "1" or "2" saving in a separate file just the first 7 columns of each row maching the criteria, with replacement of the saparators in the nearly created file from "|;#" into ";".
Just for instance this is what I have :

1;Paul;10|100;20090909#2000;1000;40;20;100;80;1000;500
2;Lara;10|100;20090909#2100;2000;80;40;100;80;1000;500
1;Carlos;24|100;20090909#2200;4000;30;50;100;80;1000;500
4;Juan;12|100;20090909#2010;6000;20;20;600;80;1000;500
3;Chis;18|100;20090909#2001;8000;50;20;100;80;1000;500

What I need in a separate file is this :

1;Paul;10;100;20090909;2000;1000
2;Lara;10;100;20090909;2100;2000
1;Carlos;24;100;20090909;2200;4000

Thanks a lot in advance for your help
Nino
# 2  
Old 09-11-2009
One way:

Code:
[house@leonov] cat test.file | grep '^[1|2]' | sed 's/\(|\|#\)/;/g' | awk -F ';' '{ print $1 ";" $2 ";" $3 ";" $4 ";" $5 ";" $6 ";" $7 }'
1;Paul;10;100;20090909;2000;1000
2;Lara;10;100;20090909;2100;2000
1;Carlos;24;100;20090909;2200;4000


Last edited by dr.house; 09-11-2009 at 01:56 PM.. Reason: Debugging (sort of ...)
# 3  
Old 09-11-2009
Another approach:

Code:
awk -F"[;|#]" '/^1/||/^2/{$7=$7"#";sub("#.*","");print}' OFS=";" file

Regards

Last edited by Franklin52; 09-11-2009 at 02:25 PM.. Reason: Misread the question
# 4  
Old 09-11-2009
or this....

Code:
awk  'BEGIN {FS="[;|#]"} /^1/ || /^2/{print $1";"$2";"$3";"$4";"$5";"$6}' file

# 5  
Old 09-11-2009
Or:

Code:
awk -F'[|;#]' '/^[12]/ {
  for (c=1; c<=max; c++) 
    printf "%s", $c (c == max ? RS : OFS)
  }' OFS=\; max=7 infile

With Perl:

Code:
perl -F'[|;#]' -lane'
  /^[12]/ and print join ";", @F[0..6]
  ' infile

With GNU awk:

Code:
awk -F'[|;#]' '/^[12]/&&NF=7' OFS=\; infile


Last edited by radoulov; 09-12-2009 at 09:59 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy the content from txt file and create a html file

I have a txt file with a list of error messages in a xml tag format, and each error message is separated with a identifier(endresult).Need to split that and copy and create a new html file.Error message has some special character. how to escape the special character and insert my data into the... (7 Replies)
Discussion started by: DevAakash
7 Replies

2. Shell Programming and Scripting

Select only the lines of a file starting with a field which is matcing a list. awk?

Hello I have a large file1 which has many events like "2014010420" and following lines under each event that start with text . It has this form: 2014010420 num --- --- num .... NTE num num --- num... EFA num num --- num ... LASW num num --- num... (9 Replies)
Discussion started by: phaethon
9 Replies

3. Shell Programming and Scripting

awk - mixed for and if to select particular lines in a data file

Hi all, I am new to AWK and I am trying to solve a problem that is probably easy for an expert. Suppose I have the following data file input.txt: 20 35 43 20 23 54 20 62 21 20.5 43 12 20.5 33 11 20.5 89 87 21 33 20 21 22 21 21 56 87 I want to select from all lines having the... (4 Replies)
Discussion started by: naska
4 Replies

4. Shell Programming and Scripting

Short program to select lines from a file based on a second file

Hello, I use UBUNTU 12.04. I want to write a short program using awk to select some lines in a file based on a second file. My first file has this format with about 400,000 lines and 47 fields: SNP1 1 12.1 SNP2 1 13.2 SNP3 1 45.2 SNP4 1 23.4 My second file has this format: SNP2 SNP3... (1 Reply)
Discussion started by: Homa
1 Replies

5. Shell Programming and Scripting

create txt file form data file

File A.txt LL07 LL07_B_1 20 LL85 LL85_A_1 40 LL85 LL85_B_1 40 LL85 LL85_C_1 30 LL37 LL37_A_1 60 LL37 LL37_B_1 20 LL37 LL37_C_1 50 I want cretae diffrent tex file base of above file Should be threee text file LL07.txt LL85.txt LL37.txt Eaach text file have below data... (2 Replies)
Discussion started by: asavaliya
2 Replies

6. Shell Programming and Scripting

create txt file form data file and add some line on it

Hi Guys, I have file A.txt File A Data AK1521 AK2536 AK3164 I want create text file of all data above and write some data on each file. want Output on below folder /home/kka/out AK1521.txt Hi Welocme (3 Replies)
Discussion started by: asavaliya
3 Replies

7. UNIX for Dummies Questions & Answers

Select text between current date and output to new txt file

Hi guys, brand new to this thread and very very new to UNIX...so go easy please! Anyway I have a file that looks like this: >>-------------------------------------------------------------------------- Date/Time/Eng. : 2012-06-22 / 00:26 / DS Reported problem : (SD) ... (5 Replies)
Discussion started by: martin0852
5 Replies

8. Shell Programming and Scripting

how to create file.txt and add current date in file content

Hey guy, how to make bash script to create foo.txt file and add current date into file content and that file always append. example: today the script run and add today date into content foo.txt and tomorrow the script will run and add tomorrow date in content foo.txt without remove today... (3 Replies)
Discussion started by: chenboly
3 Replies

9. Shell Programming and Scripting

Redirecting sql select query result to txt file

Hi Yogesh, Lucky that i caught you online. Yeah i read about DBI and the WriteExcel module. But the server is not supporting these modules. It said..."Cannot locate DBI"..."Cannot locate Spreadsheet::WriteExcel" I tried creating a simple text file to get the query output, but the... (1 Reply)
Discussion started by: dolphin123
1 Replies

10. Shell Programming and Scripting

Redirecting sql select query result to txt file

Hi , I just found you while surfing for the string 'Redirecting sql select query output from within a shell script to txt file/excel file' Could you find time sending me the code for the above question? It'll be great help for me. I have a perl file that calls the sql file... (1 Reply)
Discussion started by: dolphin123
1 Replies
Login or Register to Ask a Question