Text formatting help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Text formatting help
# 1  
Old 11-23-2011
Text formatting help

I have bunch of files with data's like below.

Code:
archive.log.0104
ar0104_akl
ar0731_rln
ar0731_rsl
M70148

I need to compile all those files into a single file(.xls file) in the below format.

1st row is file name - should come in 1st column in excel

In 2 - 4 row, all entries starts with ar***_* should come in 2nd column. (The number of entries will vary in each file)

The serial number in the last row should come in 3rd column of the excel.

Code:
archive.log.0104      ar0104_akl            M70148
                      ar0731_rln
                      ar0731_rsl

Any help will be greatly appreciated!
# 2  
Old 11-23-2011
Is archive.log.0104 just the filename, or is that string actually in the input file?

In your output, row 1 has more data than just the filename, unlike in your description. Which is correct?
# 3  
Old 11-23-2011
Quote:
Originally Posted by CarloM
Is archive.log.0104 just the filename, or is that string actually in the input file?
archive.log.0104 is just a file name / text string.

Quote:
In your output, row 1 has more data than just the filename, unlike in your description. Which is correct?
Let me try to explain clearly this time.
1st row in input file (archive.log.0104) should come in 1st column of excel file.
2-4 rows in input file (ar0104_akl
ar0731_rln
ar0731_rsl) should come in 2nd column of excel.
5th row in input file(M70148) should be in 3rd column of output excel file.

In short, the excel should look like the output code given in my earlier post.

Thanks
# 4  
Old 11-23-2011
Try this...
Assumptions : File name starts with archive and Serial number starts with M
Code:
awk '/^archive/{a=$0;next}/^M/{print a";"b[1]";",$0; for(i=2;i<=j;i++)print ";"b[i]";";delete b;j=0;next}{b[++j]=$0}' input_file > output.csv

archive.log.0104;ar0104_akl; M70148
;ar0731_rln;
;ar0731_rsl;

--ahamed
# 5  
Old 11-23-2011
Quote:
Originally Posted by ahamed101
Try this...
Assumptions : File name starts with archive and Serial number starts with M
Code:
awk '/^archive/{a=$0;next}/^M/{print a";"b[1]";",$0; for(i=2;i<=j;i++)print ";"b[i]";";delete b;j=0;next}{b[++j]=$0}' input_file > output.csv

archive.log.0104;ar0104_akl; M70148
;ar0731_rln;
;ar0731_rsl;

--ahamed
Ahamed,

I'm getting the below error.

Code:
 syntax error The source line is 1.
 The error context is
                /^archive/{a=$0;next}/^M/{print a";"b[1]";",$0; for(i=2;i<=j;i++)print ";"b[i]";";delete >>>  b; <<< 
 awk: The statement cannot be correctly parsed.
 The source line is 1.

# 6  
Old 11-23-2011
which is your OS? If solaris, use nawk

Try this...
Code:
awk '/^archive/{a=$0;next}/^M/{print a";"b[1]";",$0; for(i=2;i<=j;i++){print ";"b[i]";"}j=0;next}{b[++j]=$0}' input_file > output.csv

--ahamed
# 7  
Old 11-23-2011
OS is HP-UX. awk works fine. But it gives error.


EDIT: It works with the latest command. Thanks. What if the serial number starts with other letter?and What if the there is more than 1 serial number? I have some serial number starts with 'N' and some files have more than one serial number. I want the extra serial number to be in another column.

Last edited by vasanth_123; 11-23-2011 at 11:15 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Text formatting

I know I've been away from the forums for past couple of months. But I am back now : ) I used to hit ctrl+B and ctrl+I after selecting the text to make text bold or italic respectively. (Pressing the keys would add and tags etc) However, that is not working anymore. Did I miss something? (13 Replies)
Discussion started by: clx
13 Replies

2. Shell Programming and Scripting

Help Me with the formatting of text

Hi, I am new to this forum; I need a help for my scripting problem. I have made a script in Unix which is extracting a report but the issue is that report is not in a proper format. Original Report Ex: Field 1....................... a b c d e f g Field 2............. @ID.@ID Field... (4 Replies)
Discussion started by: tush
4 Replies

3. Shell Programming and Scripting

Help with Text formatting

I am generating the o/p as: BLANSWER 112747 112747 TBLQSTN 983 692 INITIATIVE 35 35 PAIGN 3122 3538 IGNCONTACT 90136 93534 IGNGROUP 27 27 AIGNSTEP 16899 20437 AIGNTYPE ... (1 Reply)
Discussion started by: karumudi7
1 Replies

4. Shell Programming and Scripting

Text formatting

A folder is having n number of files each file is having column names in it .Hence using below code . for file in /xxx/sss/* do filename=$( basename $file ) sed -e '1,2d; $d; /^*$/d; /selected\.$/d' ${file} | \ sed -e '1s/^/INSERT INTO '${filename}' VALUES (/; $!s/$/,/; $s/$/);/'... (6 Replies)
Discussion started by: rocking77
6 Replies

5. Shell Programming and Scripting

Text formatting

I have an input file as below. 1 Sanjib Gayen 2 Chetan Jadhav 3 Vijaykumar Uddi 4 Pinaki Sarkar I want to generate an output file as below. 1-Sanjib Gayen 2-Chetan Jadhav (4 Replies)
Discussion started by: R0H0N
4 Replies

6. UNIX for Dummies Questions & Answers

Formatting TEXT

Hello, I have the following lines in a text file: /var/spool/postfix/defer/1/15C86B0547C /var/spool/postfix/defer/1/19AD1B054A2 /var/spool/postfix/defer/2/25A16B05493 /var/spool/postfix/defer/6/626FBB05496 /var/spool/postfix/defer/6/634D4B0544A /var/spool/postfix/defer/6/6A8ACB05499... (2 Replies)
Discussion started by: mojoman
2 Replies

7. Shell Programming and Scripting

text formatting

Hi, any idea input: mr.smith stvenson: /address #/tel no/ personal data profile. mrs.smith stevenson: /address #/tel no/occupation/ personal data profile. output: mr.smith stvenson address #: tel no: personal data profile. mrs.smith stevenson address #: tel no: occupation:... (7 Replies)
Discussion started by: kenshinhimura
7 Replies

8. UNIX for Dummies Questions & Answers

Text file formatting

Hi all! I'm new in unix, and faced with some difficulties. So I have text file f.e. "textfile" which contains rows like: aaa bbb ccc ddd How could I format it, so the file looks like: aaabbb cccddd Thanks in andvance (5 Replies)
Discussion started by: consta.v
5 Replies

9. UNIX for Dummies Questions & Answers

Text Formatting

I have a file like this with 1 lac lines. 1 2 3 4 5 6 7 8 9 0 I would like to format the above text file 1 2 3 4 5 6 7 8 9 0 (7 Replies)
Discussion started by: aravindj80
7 Replies

10. Shell Programming and Scripting

formatting text

Hi, I am having a file containing entries like: .iso.org.dod.internet.mgmt.mib-2.system.sysName.0 .iso.org.dod.internet.mgmt.mib-2.system.sysLocation.0 .iso.org.dod.internet.mgmt.mib-2.system.sysServices.0 .iso.org.dod.internet.mgmt.mib-2.system.sysORLastChange.0... (16 Replies)
Discussion started by: esham
16 Replies
Login or Register to Ask a Question