How can I get certain records from this file format?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How can I get certain records from this file format?
# 8  
Old 06-03-2013
Hmm I am also newbie to Unix... still learning it Smilie

I just tried below command on my test file

awk -F"|" 'BEGIN{RS="^M"}NR<=5{print}' learn.999

And it gave me output as:

Code:
6232|12341|8144836458|20110719132528|0
4124|5963|3022610000|20011023205510|0
7606|10858|3022560001 |20071012222359|0
7606|10257|3022560002|20061205202926|0
6664|7208|7247129469|20020927162412|0

I just displayed only for 5 records. You can try with 1000000 records.
Do not copy paste this command. Otherwise you will end up writing "^M" as literal characters instead of control characters. You need to do - Ctrl + V + M to get "^M". See if this helps...
# 9  
Old 06-03-2013
I modified your script to 'BEGIN{RS="\015"} it did work. however, it did put each record on a separate line ! However, I want to be in one line like the original file is !
# 10  
Old 06-03-2013
That's good.
in order to print them on one line, can you please try below:

awk -F"^M" '{for(i=1;i<=1000000;i++) printf("%s", $i)}' learn.999

I have assumed here that your input file will always contain at least 1000000 records.
# 11  
Old 06-03-2013
The ^M is nothing but carriage Return. In unix, it will be displayed as ^M.

You cannot run these commands on a zipped file. Smilie

If you want to avoid, ^M and insert a new line there use below command,
Code:
tr '\r' '\n' < inputfile > outputfile

You may need to have enough space to accommodate the new file too.
Code:
awk 'BEGIN { RS="\r";ORS="\n" } NR<=1000000 {print}' inputfile  > outputfile

(I am not good with awk. I just googled to build this command. Smilie Smilie)
# 12  
Old 06-04-2013
Quote:
Originally Posted by juzz4fun
That's good.
in order to print them on one line, can you please try below:

awk -F"^M" '{for(i=1;i<=1000000;i++) printf("%s", $i)}' learn.999

I have assumed here that your input file will always contain at least 1000000 records.

This one did not work. It did not insert ^M at the end of each record ! All are in one record wrapped and no ^M between each one !!

---------- Post updated at 01:10 PM ---------- Previous update was at 12:59 PM ----------

However, this worked to convert the timmed file back to the original format:


awk 'BEGIN { RS="\n";ORS="\r" } NR<=1000000 {print}' inputfile > outputfile


Issue resolved.

Thank you all for your valuable information. Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Separate records of a file on 2 types of records

Hi I am new to shell programming in unix Please if I can provide help. I have a file structure of a header record and "N" detail records. The header record will be the total number of detail records I need to split the file in 2: One for the header Another for all detail records Could... (1 Reply)
Discussion started by: jamcogar
1 Replies

2. Shell Programming and Scripting

Multiple records need to convert UNIXtime to human readable datatime and all output in one format

Hello Experts, Below is the record i have: sample data attached I want this record of each row to be in single line and there are multiple rowise unixtime mentioned e.g 11996327 , This needs to be converted to Human readdable data and time from multiple rows Can you help me , it will be... (10 Replies)
Discussion started by: manishK
10 Replies

3. UNIX for Dummies Questions & Answers

Printing records in different format

Hi all, I have a input file say record.txt hostname IP_address Port_No Version A 10.10.10.1 80 6.02 B 10.10.10.2 81 6.03 C 10.10.10.3 82 6.04 row 1 has 4 field headings : hostname, IP_address, Port_No and Version. and from 2nd row onwards the actual records start. now i need to... (2 Replies)
Discussion started by: PranavEcstasy
2 Replies

4. Shell Programming and Scripting

Deleting duplicate records from file 1 if records from file 2 match

I have 2 files "File 1" is delimited by ";" and "File 2" is delimited by "|". File 1 below (3 record shown): Doc1;03/01/2012;New York;6 Main Street;Mr. Smith 1;Mr. Jones Doc2;03/01/2012;Syracuse;876 Broadway;John Davis;Barbara Lull Doc3;03/01/2012;Buffalo;779 Old Windy Road;Charles... (2 Replies)
Discussion started by: vestport
2 Replies

5. Shell Programming and Scripting

Converting windows format file to unix format using script

Hi, I am having couple of files which i used to copy from windows to Linux, so now in case of text files (CTRL^M) appears at end of line. I know i can convert this windows format file to unix format file by running dos2unix. My requirement here is that i want to do it automatically using a... (5 Replies)
Discussion started by: sarbjit
5 Replies

6. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: xxx@yahoo.com Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

7. Shell Programming and Scripting

Format & re-arrange the records

Data on my input file : Ac1n1s1c2n2s2XPd1r1e1t1d2r2e2t2d3r3e3t3d4r4e4t4RT Bh1k1p1h2k2p2NTq1y1f1m1q2y2f2m2q3y3f3m3q4y4f4m4ZN and i want the output to be: Ac1n1s1XPd1r1e1t1RT Ac1n1s1XPd2r2e2t2RT Ac1n1s1XPd3r3e3t3RT Ac1n1s1XPd4r4e4t4RT Ac2n2s2XPd1r1e1t1RT Ac2n2s2XPd2r2e2t2RT... (6 Replies)
Discussion started by: rlmadhav
6 Replies

8. UNIX for Dummies Questions & Answers

Use records from one file to delete records in another file

file_in_1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 file_in_2: 9 10 11 12 21 22 23 24 1 2 3 4 17 18 19 20 file_out: (5 Replies)
Discussion started by: kenneth.mcbride
5 Replies

9. UNIX for Dummies Questions & Answers

To convert multi format file to a readable ascii format

Hi I have a file which has ascii , binary, binary decimal coded,decimal & hexadecimal data with lot of special characters (like öƒ.ƒ.„İİ¡Š·œƒ.„İİ¡Š· ) in it. I want to standardize the file into ASCII format & later use that as source . Can any one suggest a way a logic to convert such... (5 Replies)
Discussion started by: gaur.deepti
5 Replies

10. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies
Login or Register to Ask a Question