LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-10-2010, 07:45 AM   #1
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Rep: Reputation: 16
stream editing inside the while loop


I have a fallowing ASCII text file:
Code:
Fa0/2 - server1 / testing
Fa0/4 - server2 / production
Fa0/5 - server3 / production
and I would like to make a bash oneliner, which takes this script as an input and produces fallowing output:

Code:
configure terminal
interface Fa0/2
description server1 / testing
exit
interface Fa0/4
description server2 / production
exit
interface Fa0/5
description server3 / production
exit
I made this:
Code:
while read line; do printf "configure terminal\ninterface $line\ndescription\nexit\n"; done < /tmp/list
However, how can I do stream editing inside the loop?
 
Old 03-10-2010, 07:53 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
In place of "$line", try this:
$(echo $line|awk '{print $1}')
 
Old 03-10-2010, 08:04 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
This would work, too
Code:
while read line; do printf "configure terminal\ninterface ${line%% *}\ndescription ${line#*- }\nexit\n"; done < /tmp/in.txt

Last edited by catkin; 03-10-2010 at 08:05 AM. Reason: [CODE] tags didn't work ???
 
Old 03-10-2010, 08:04 AM   #4
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Code:
awk -F"-" '{print "configure terminal\n","interface ", $1,"\n","description  ", $2,"\n", "exit;"}'
 
Old 03-10-2010, 08:18 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
You can either use the read statement to assign more variables at once or to fill an array. For example:
Code:
$ while read -a line
> do
>   echo configure terminal
>   echo interface ${line[0]}
>   echo description ${line[2]} ${line[3]} ${line[4]}
>   echo exit
> done < /tmp/list
 
Old 03-10-2010, 08:34 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by m4rtin View Post
However, how can I do stream editing inside the loop?
Just noticed you talked about "stream editing". Not sure about that, but if you mean that the input file is continuously updated and you want to print out information on-the-fly, the awk solution is the most suitable (in conjunction with tail -f). For example:
Code:
tail -f /tmp/list | awk -F- '{printf "configure terminal\ninterface %s\ndescription%s\nexit\n", $1, $2}'
 
Old 03-10-2010, 08:11 PM   #7
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Original Poster
Rep: Reputation: 16
Thanks! Basically all those solutions will work just fine. However, just out of interest, is it possible to make a bash script, which waits for stdin input from clipboard? I mean fallowing scenario:

1) I copy this
Code:
Fa0/2 - server1 / testing
Fa0/4 - server2 / production
Fa0/5 - server3 / production
part from an email in my Thunderbird Inbox with Ctrl+C.

2) I have a script open in terminal emulator, which waits for me to paste this

Code:
Fa0/2 - server1 / testing
Fa0/4 - server2 / production
Fa0/5 - server3 / production
from my clipboard to a terminal emulator(with Ctrl+Shift+V)

3) In terminal emulator window

Code:
configure terminal
interface Fa0/2
description server1 / testing
exit
interface Fa0/4
description server2 / production
exit
interface Fa0/5
description server3 / production
exit
...appears.

Is such "real-time" processing possible at all with bash script?
 
Old 03-11-2010, 03:26 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
With a little modification of one of the codes suggested above you can achieve a similar behaviour. The requirement can be rephrased as: don't echo the standard input (coming from pasting a selection) and echo my formatted output. The option -s of read can accomplish the first part, hence...
Code:
#!/bin/bash
while true
do
  read -s -a line
  echo configure terminal
  echo interface ${line[0]}
  echo description ${line[2]} ${line[3]} ${line[4]}
  echo exit
done | tee -a test.log
You have to press enter, anyway. The tee part at the end is just to save the output to a file (optional).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash: variables inside while loop J_Szucs Programming 5 07-19-2009 08:24 AM
AWK: problem getline inside while loop jjhall Programming 2 02-27-2008 03:19 PM
trouble with expr inside a while loop farkus888 Linux - General 5 04-06-2007 03:19 AM
Octet-Stream - Stuck in a loop!?* floydking Linux - Software 1 04-04-2006 09:48 AM
for loop inside select box opioid Programming 1 03-17-2005 02:22 PM


All times are GMT -5. The time now is 07:05 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration