LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-05-2013, 12:17 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
formatting output in bash


So i have a list -
cat /tmp/foo
Code:
InvSellSide42
onSellSide42
CapSellSide42
gerSellSide42
FortSellSide42
M42BuySide
MeSellSide42
PSPSSide40
and if you cat out just one of these files it looks something like this -

Code:
[timers/plugin_timer_0_0]
action = doStart
trigger = 0 35 18 ? * 1,2,3,4,5;

[timers/plugin_timer_1_0]
action = doStop; doReset
trigger = 0 00 18 ? * 2,3,4,5,6;
All i need to get is the name, start and stop times.
which i can do - but it gets printed out like this.
Code:
ASellSide40,
0 35 18,
0 00 18
BSellSide42,
0 00 18,
0 50 17
CSellSide42,
0 30 19,
0 30 16
ESellSide42,
0 30 07,
0 30 17
I need it printed out like this:
Code:
ASellSide40, 0 35 18, 0 00 18
BSellSide42, 0 00 18, 0 50 17
CSellSide42, 0 30 19, 0 30 16
ESellSide42, 0 30 07, 0 30 17
here is the code - the code runs ok, i just do not know how to format the print out.
Code:
#!/bin/bash
for i in $(cat /tmp/foohoo)
do
dostart=$(cat /tmp/conf/$i.ini  | egrep -A2 "action = doStart")
parse_dostart=$(echo "$dostart" | perl -nle 'print /trigger\s=\s(\w{1,2}\s\w{1,2}\s\w{1,2})/')
dostop=$(cat /opt/ullink/service/ulbridge/conf/$i.ini  | egrep -A2 "action = doStop")
parse_dostop=$(echo "$dostop" | perl -nle 'print /trigger\s=\s(\w{1,2}\s\w{1,2}\s\w{1,2})/')
#printf "%10s\t%10d\t%10d \n"  $i, $parse_dostart, $parse_dostop
printf "$i,\t$parse_dostart,\t $parse_dostop\n"
done

Last edited by casperdaghost; 09-05-2013 at 12:57 PM.
 
Old 09-05-2013, 12:49 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
I would use awk,
Code:
for file in $(cat /tmp/foo); do
    awk -v File=$file 'BEGIN{printf File}
                    /trigger/{printf ", "$3" "$4" "$5}
                    END{printf"\n"}' $file
done

It does make a number of assumptions,
one being foo has full path
another is that each file in foo has the correct 'format'

Last edited by Firerat; 09-05-2013 at 12:51 PM.
 
Old 09-05-2013, 12:56 PM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
Is there a way to do it with printf formatting?
It can't be that hard. I have tried google - cant really find a explanation.
 
Old 09-05-2013, 01:08 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
I don't know perl.. but try removing the -l flag ( which processes line ends ) that is where your 'extra' end of lines are coming from
 
Old 09-05-2013, 01:28 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I agree with Firerat that awk, perl or ruby would possibly be better choices for the entire code.

My personal choice if using bash though would be to go all bash:
Code:
while read -r file_name
do
    echo -n "$file_name"

    while read -r line
    do
        [[ $line =~ trigger ]] && echo -n ", ${line:10:7}"
    done<"/tmp/conf/$file_name.ini"
    echo
done</tmp/foohoo
 
1 members found this post helpful.
Old 09-05-2013, 01:57 PM   #6
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
removing the newline - l from the perl statement worked.

how do i mark this as solved - and how do i make certain that everybody who answered this thread gets some points.
 
Old 09-05-2013, 02:02 PM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
to mark as solved ... select the dropdown under thread tools (above the first post) and select mark as solved.

to give points click on 'Did you find this post helpful? yes
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Output formatting . pinga123 Linux - Newbie 6 03-25-2011 03:04 AM
BASH: Help with formatting Output from a script SilversleevesX Programming 4 08-23-2010 06:59 PM
Is this BASH tweak possible? (Formatting the output) Rob00 Programming 6 10-19-2009 12:43 AM
bash - can you turn off output formatting jrfk2 Linux - General 2 01-17-2007 01:01 PM
Bash Select Function: Formatting Output mooreted Linux - General 2 03-28-2004 06:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration