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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh - Read input from file and output CSV i same row
# 1  
Old 02-15-2018
ksh - Read input from file and output CSV i same row

Hello

I have the following output and want the output to look:

FROM:
Code:
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 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
     42 output errors, 0 collisions, 3 interface resets

TO: (in CSV format)

Code:
Interface, Input Errors, Output Errors
GigabitEthernet0/0, 1, 275
GigabitEthernet0/1, 0, 42

When I put the above in a for loop I get staggered outputs as I'm greping on the turn of every loop

Here's my script

Code:
for line in $(cat $DATAFILE/$rname.intfs)
do
     intf=$(echo $line | grep "line protocol is " | awk '{print $1}')
     inerrs=$(echo $line | grep "input error" | sed 's/^[ \t]*//;s/[ \t]*$//' | awk '{print $1}')
     outerrs=$(echo $line | grep "output error" | sed 's/^[ \t]*//;s/[ \t]*$//' | awk '{print $1}')
     echo "$intf,$inerrs,$outerrs" >intf.csv
done

Any help is appreciated

thanks

Jay

Last edited by rbatte1; 02-15-2018 at 07:57 AM.. Reason: Added CODE tags
# 2  
Old 02-15-2018
With sed:
Code:
sed 's/ .*$/,/;N;s/ input.*$/,/;N;s/ output.*$//;s/\n//g' < $DATAFILE/$rname.intfs

Output:
Code:
GigabitEthernet0/0,     1,     275
GigabitEthernet0/1,     0,     42

Works for me on Ubuntu 16.04.03, GNU sed 4.2.2 but it should work for any sed.

Andrew
# 3  
Old 02-15-2018
Welcome JayJay2018,

Please wrap all code, files, input & output/errors in CODE tags (highlight and press the CODE toolbar button)
It is easier to read as code because it is mono-spaced and preserves multiple spaces for indenting and fixed-width data. I have adjusted your initial post for you. If you click on the edit button, you can see how.


My first suggestion is to lose the $(cat $DATAFILE..... bit. It is far more efficient to have your loop like this:-
Code:
while read line
do
   whatever
   echo "Here is my output"
done < $DATAFILE/$Rname.intfs > intf.csv

The output is written as one operation to the file rather than lots of separate statements, which (by the way) overwrite the file, so you will only actually see the last record being output. You also write the output line for every input line, so you will get a variety of output being built up as records are read. As the values are not reset, after the first complete 'chunk' of the output, the next will start with the old values filled in which is probably confusing.

If you are certain of your input format, you can probably do this:-
Code:
while read firstline
do
   read secondline
   read thirdline
   # Some processing here
   echo                                       # Whatever you have built
done < $DATAFILE.......  > intf.csv

When it comes to processing your lines to get the values you want, you are also calling far more steps than you need. With ksh or bash, you can split up the line as you read it. You need just the first item of each line, so this brings my first suggested code to this:-
Code:
while read intf junk
do
   read inerrs  junk
   read outerrs junk

   echo "$intf, $inerrs, $outerrs"
done < $DATAFILE/$rname.intfs > intf.csv


Does that help?

If I've confused you, I apologise. If the script produces errors, please paste them in to the thread, wrapped in CODE tags of course, and I will have another think.



Kind regards,
Robin

Last edited by rbatte1; 02-15-2018 at 08:18 AM.. Reason: Added spaces in output as required.
# 4  
Old 02-15-2018
sed works !

Robin, Andrew, thanks for your reply

The sed solution works perfectly! Genius! Smilie

thanks again

Jay
# 5  
Old 02-15-2018
Try also
Code:
awk 'BEGIN {print "Interface, Input Errors, Output Errors"} {printf "%s%s", $1, !(NR%3)?RS:", "}' file
Interface, Input Errors, Output Errors
GigabitEthernet0/0, 1, 275
GigabitEthernet0/1, 0, 42

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python or Shell script to Grep strings from input file and output in csv format

Hi Experts, I am writing a python script to grep string from file and display output in csv file as in attached screenshot https://drive.google.com/file/d/1gfUUdfmQma33tz65NskThYDhkZUGQO0H/view Input file(result_EPFT_config_device) Below is the python script i have prepared as of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

ksh to read input until QUI

Hello all I'm looking to write a simple script (ksh/sh/bsh) to read user input and write it to a file (adding each time) until the user enters QUIT at which point I'm hoping to ask some more questions. Any help much apprecited (2 Replies)
Discussion started by: Grueben
2 Replies

3. Shell Programming and Scripting

ksh loop to read input until QUIT

Hi I'm looking to write a simple ksh loop reading user input (and write it to a file) until the user enters QUIT at which point I want it to continue. Does anyone have an example of this type of loop? Any help much appreciated Cheers (2 Replies)
Discussion started by: Grueben
2 Replies

4. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

5. Shell Programming and Scripting

Read file from input and redirect to output file

Hi , i am having an file which contains 5 file_name data, i need to read the file name and will perform certain operation and generate out file names with named as 5 individual file_names for eg: file.txt contains file_name1.txt|hai file_name2.txt|bye file_name3.txt|how... (3 Replies)
Discussion started by: rohit_shinez
3 Replies

6. Shell Programming and Scripting

ksh using input file with output going to same file name and location

I've been asked if I can write a "quick" little ksh script that will do the following: java java_class_file /dir/input_file.xml /dir/output_file.xml I'm a complete newbie with ksh so any help would be appreciated. This is on AIX and java is found in /usr/java5/jre/bin/java (4 Replies)
Discussion started by: newbie_ksh
4 Replies

7. Shell Programming and Scripting

Read multiple input from CLI :ksh

Hi folks.. i got a requirement to red multiple directories from STDIN and store them to a variable. ex:- echo "Enter directory to add:" echo " Enter directory to add:" read value till there is input and when there is no input close the read loop and store variable into an array ... (1 Reply)
Discussion started by: bangaram
1 Replies

8. Shell Programming and Scripting

[KSH] Creating automatic variable from read input

Hello there, I am posting to seek help with a KSH script, I am making a simple calculation program where the user can enter as many numbers as they like, I am getting their input using the read command, however I am not sure how to repeat doing this and storing the input in to new variables... (7 Replies)
Discussion started by: pandapowerbox
7 Replies

9. Shell Programming and Scripting

Read a CSV file and generate SQL output

Friends, This is what I need: I will pass a CSV file as an input, and I want my shell to be reading that CSV file, and based on the parameters it should generate SQLs and write those SQL in a different file in the same location. I'm new to Shell scripting. I'm currently working on a... (25 Replies)
Discussion started by: Ram.Math
25 Replies

10. Shell Programming and Scripting

to read a CSV file and generate SQL output

Friends, This is what I need: I will pass a CSV file as an input, and I want my shell to be reading that CSV file, and based on the parameters it should generate SQLs and write those SQL in a different file in the same location. I'm new to Shell scripting. I'm currently working on a... (1 Reply)
Discussion started by: Ram.Math
1 Replies
Login or Register to Ask a Question