Two Input Lines Into Single Output Line (CSV)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Two Input Lines Into Single Output Line (CSV)
# 15  
Old 07-24-2011
Can you post output of:
Code:
cat -eT switch1.txt

This User Gave Thanks to bartus11 For This Post:
# 16  
Old 07-24-2011
It never takes long before there's something I can't do on OS X that could normally be done on a true *nix box. I couldn't do the -T switch but I could do a lower-case -t switch. For whatever it's worth, here's the output from that:

$ cat -eT switch1.txt | awk '/ntp/'
cat: illegal option -- T
usage: cat [-benstuv] [file ...]

$cat -et switch1.txt | awk '/ntp/'
ntp server 10.10.250.2^M$
ntp server 10.10.250.1 prefer^M$

Tell you anything valuable?
# 17  
Old 07-24-2011
Does your Mac have dos2unix utility?
# 18  
Old 07-24-2011
No, but what do you think about the final two posts in this thread:

Convert end-of-line characters (revisited) - Mac OS X Hints

It appears there're many ways to go about this, so if you could be so kind as to point out what I'm to replace (and what with), I'm sure I can get it done. Heck, maybe I can get it done during the creation of these .txt files (I'm generating them via a TCL script that calls Expect and does a bunch of interactive stuff to give me not only the configs, but a bunch of 'show processor cpu history' and so forth). Evidently that process is fouling up the EOL or something?
# 19  
Old 07-24-2011
Try this:
Code:
perl -nle 's/\x0d$//;printf "$ARGV," if $.==1;printf "$_," if /ntp server/;if (eof){$.=0;printf "\n"}' * > out.csv

This User Gave Thanks to bartus11 For This Post:
# 20  
Old 07-24-2011
Yes, the above does work quite nicely! It gives me an output file in about one second, which isn't bad considering I have roughly 200 .txt files (some of which are quite large) in the directory.

However, I'm even less Perl aware than I am grep/awk aware, so I really have no idea what the above does or how. I have many, many applications for such as script, so I'm hopeful to walk away from all of this with a template that can be adapted/evolved. Not knowing much about Perl at all, I fear I would have to seek help getting anything new out of the above. Can you please help me to understand it, at least on a high level? I won't be the least bit offended if you say "no" - you've already given of your time.

agama,

I suppose this whole EOL thing made perfect sense to you all along. Is there now a tweak to your code that would work? I'm not sure what I have to do in order to get xargs going but I'll shortly find out...

---------- Post updated at 01:04 PM ---------- Previous update was at 01:03 PM ----------

Oh, and can I limit the above to just files in the directory with .txt extensions?
# 21  
Old 07-24-2011
You don't need to use a stand alone utility to trash the carriage-return characters when using an awk to parse your data. Just add an extra gsub() and that should work:

Code:
awk '
    BEGIN { n = 0; k = 1; }

    FILENAME != last {
        fn = FILENAME;
        gsub( ".txt", "", fn );
        printf( "%s%s%s", k ? "" : ",", n ? "\n" : "",  fn );
        last = FILENAME;
        n = 1;     # signal need for newline before fn (prevent lead newline)
        k = 0;     # when set, signals comma
    }

    /ntp server/ || /ntp peer/ {
        gsub( "\r", "", $0 );           # ditch the charriage return
        printf( ",%s", $0 );
        k = 1;
    }

    END {
        printf( "%s\n", k ? "" : "," );
    } '  *.txt

This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

ksh - Read input from file and output CSV i same row

Hello I have the following output and want the output to look: FROM: GigabitEthernet0/0 is up, line protocol is up 1 input errors, 0 CRC, 0 frame, 1 overrun, 0 ignored 275 output errors, 0 collisions, 3 interface resets GigabitEthernet0/1 is up, line protocol is up 0... (4 Replies)
Discussion started by: JayJay2018
4 Replies

3. Shell Programming and Scripting

How do I split a single-line input into five lines?

Example input: John:Shepherd:770-767-4040:U.S.A:New York Mo Jo:Jo Jo: 666-666-6666:U.S.A:Townsville Expected Output: First Name: John Last Name: Shepherd Phone Number: 770-767-4040 Country: U.S.A State: New York First Name: Mo Jo Last Name: Jo Jo Phone Number: 666-666-6666... (10 Replies)
Discussion started by: Camrikron
10 Replies

4. UNIX for Dummies Questions & Answers

Need help combining txt files w/ multiple lines into csv single cell - also need data merge

:confused:Hello -- i just joined the forums. I am a complete noob -- only about 1 week into learning how to program anything... and starting with linux. I am working in Linux terminal. I have a folder with a bunch of txt files. Each file has several lines of html code. I want to combine... (2 Replies)
Discussion started by: jetsetter
2 Replies

5. Shell Programming and Scripting

turning output of two lines into one CSV line

Hi, I am attempting to use sed on linux to do something trivial. I am also too embarassed to show you what I have tried so far! What I am trying to do should be trivial, if I knew what I was doing, but I don't. Would someone please help me? Here is my problemI have a ASCII file that has the... (4 Replies)
Discussion started by: Jon8987z
4 Replies

6. Shell Programming and Scripting

Input two variable on single line

Can we input two variable on single line that separate by space example user input "list jpg" it will list all jpg files in current directory (3 Replies)
Discussion started by: guidely
3 Replies

7. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

8. Shell Programming and Scripting

Break lines up into single lines after each space in every line

It sounds a bit confusing but what I have is a text file like the example below (without the Line1, Line2, Line3 etc. of course) and I want to move every group of characters into a new line after each space. Example of text file; line1 .digg-widget-theme2 ul { background: rgb(0, 0, 0) none... (7 Replies)
Discussion started by: lewk
7 Replies

9. Shell Programming and Scripting

single line input to multiple line output with sed

hey gents, I'm working on something that will use snmpwalk to query the devices on my network and retreive the device name, device IP, device model and device serial. I'm using Nmap for the enumeration and sed to clean up the results for use by snmpwalk. Once i get all the data organized I'm... (8 Replies)
Discussion started by: mitch
8 Replies

10. Shell Programming and Scripting

Need output in different lines not in one single line

I am getting the coutput like this as show below in one single line, where as the command is executed is several lines and the output should also be requied in several lines, not in one single line. Anyone any idea? p4 opened -a | grep *locked* | awk '{ printf $8 }' >/tmp/aa $ cat... (1 Reply)
Discussion started by: csaha
1 Replies
Login or Register to Ask a Question