LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 11-29-2010, 02:49 AM   #1
threeonethree
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Rep: Reputation: 0
bash write to end of each line


i have a text file like

abc abc abc abc
abc abc abc abc
--- --- --- ---

i want to add a string xyz to end of each line..


abc abc abc abc xyz
abc abc abc abc xyz
abc abc abc abc xyz
--- --- --- --- xyz
--- --- --- --- xyz


how do i do this in bash?
 
Old 11-29-2010, 03:02 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Code:
sed 's/$/ xyz/' file.txt
$ means end-of-line in this case.
When you're sure the output is what you want, add the -i flag to edit the file in-place.

Edit: Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

Last edited by David the H.; 11-29-2010 at 03:04 AM.
 
Old 11-29-2010, 03:09 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Oh, and here's how you can do it entirely with bash built-ins. Simply loop through each line of the file and print it with the extra string added. You have to pipe the output into a new file though.
Code:
while read line; do
  echo "$line xyz"
done  <file.txt  >newfile.txt
 
Old 11-29-2010, 03:54 AM   #4
threeonethree
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Original Poster
Rep: Reputation: 0
thanks for the help

Code:
cd "$temp" || exit 1
while read file
do
while read line; do
if [[ grep --silent Cisco $temp/$file ]]; then
        echo "$line cisco"
    elif [[ grep --silent Maipu $temp/$file ]];then
        echo "$line maipu"
    elif [[ grep --silent V3.90 $temp/$file ]];then
        echo "$line zyxel3.9"
    else
        echo "$line Invalidtype"
    fi
  done  </home/noc/Desktop/autotel/temp1/RESULT >/home/noc/Desktop/autotel/temp1/FINALRESULT
done <<< $(ls -1)
i am trying to read a file and check specific patterns in it. if a pattern is found i want to write to the end of line of the result file..


output i get

./tacacs: line 35: conditional binary operator expected
./tacacs: line 35: syntax error near `--silent'
./tacacs: line 35: `if [[ grep --silent Cisco $file ]]; then'
 
Old 11-29-2010, 04:34 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
This would be related to your other question you already have open so i would take it up there as your question here has been answered.
 
Old 11-29-2010, 05:10 AM   #6
threeonethree
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Original Poster
Rep: Reputation: 0
if [ ! grep --silent Cisco "$temp/$file" | echo "$?" ] ; then

why cant this work ^^ ? how can i fix it ?
 
Old 11-29-2010, 05:36 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You need to close this question as it has been SOLVED.
 
Old 11-29-2010, 06:09 AM   #8
threeonethree
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Original Poster
Rep: Reputation: 0
I would be happy to close this thread once it is solved.

I am trying to find the device type eg cisco maipu zyxel etc using bash script and expect..

till now what i have done is that my script checks vty connections to a device .. runs show version command and saves everything in a temp file which is named the same as the ip address of the device.

also makes a result file which has something like this

device ip vty status
--------- ----------
--------- ----------

etc


the files in temp are such that
filename 127.0.0.1
cisco if show version output cisco
or zyxel or so on ...

what i need to do is to grep the pattern "cisco / zyxel / maipu" if it exists in the file of specific device ( named same as ip address ) and put the device type in the new result file such that

device ip vty status device type
--------- ----------- ----------
---------- --------- ------------

Code:
cd "$temp" || exit 1
while read file; do
  while read line; do
    if [ ! `grep --silent Cisco "$temp/$file" | echo "$?"` ] ; then
        echo -n "$line cisco"
        continue
    elif [ ! `grep --silent Maipu "$temp/$file" | echo "$?"` ] ; then
        echo -n "$line maipu"
        continue
    elif [ ! `grep --silent V3.90 "$temp/$file" | echo "$?"` ] ; then
        echo -n "$line zyxel3.90"
        continue
    else
        echo -n "$line Invalidtype"
        continue
    fi
  done  < "$HOME/Desktop/autotel/temp1/RESULT" > "$HOME/Desktop/autotel/temp1/FINALRESULT"
done <<< $(ls -1)
grep can not match the single ip address one by one..
Code:
grep: /home/noc/Desktop/autotel/temp/10.169.247.34 10.173.73.1 10.173.73.254 71.11.162.253 71.11.162.254: No such file or directory
grep: /home/noc/Desktop/autotel/temp/10.169.247.34 10.173.73.1 10.173.73.254 71.11.162.253 71.11.162.254: No such file or directory
grep: /home/noc/Desktop/autotel/temp/10.169.247.34 10.173.73.1 10.173.73.254 71.11.162.253 71.11.162.254: No such file or directory
grep: /home/noc/Desktop/autotel/temp/10.169.247.34 10.173.73.1 10.173.73.254 71.11.162.253 71.11.162.254: No such file or directory
grep: /home/noc/Desktop/autotel/temp/10.169.247.34 10.173.73.1 10.173.73.254 71.11.162.253 71.11.162.254: No such file or directory
grep: /home/noc/Desktop/autotel/temp/10.169.247.34 10.173.73.1 10.173.73.254 71.11.162.253 71.11.162.254: No such file or directory
grep: /home/noc/Desktop/autotel/temp/10.169.247.34 10.173.73.1 10.173.73.254 71.11.162.253 71.11.162.254: No such file or directory
grep: /home/noc/Desktop/autotel/temp/10.169.247.34 10.173.73.1 10.173.73.254 71.11.162.253 71.11.162.254: No such file or directory
grep: /home/noc/Desktop/autotel/temp/10.169.247.34 10.173.73.1 10.173.73.254 71.11.162.253 71.11.162.254: No such file or directory
grep: /home/noc/Desktop/autotel/temp/10.169.247.34 10.173.73.1 10.173.73.254 71.11.162.253 71.11.162.254: No such file or directory
grep: /home/noc/Desktop/autotel/temp/10.169.247.34 10.173.73.1 10.173.73.254 71.11.162.253 71.11.162.254: No such file or directory
grep: /home/noc/Desktop/autotel/temp/10.169.247.34 10.173.73.1 10.173.73.254 71.11.162.253 71.11.162.254: No such file or directory
grep: /home/noc/Desktop/autotel/temp/10.169.247.34 10.173.73.1 10.173.73.254 71.11.162.253 71.11.162.254: No such file or directory
grep: /home/noc/Desktop/autotel/temp/10.169.247.34 10.173.73.1 10.173.73.254 71.11.162.253 71.11.162.254: No such file or directory
grep: /home/noc/Desktop/autotel/temp/10.169.247.34 10.173.73.1 10.173.73.254 71.11.162.253 71.11.162.254: No such file or directory
this is the final output file..
Code:
10.173.73.254 - vty login ok - version - eth0 - admin - 1234
 Invalidtype10.169.247.34 - vty login ok - version - eth0 - tulipadmin - pl3p30tcnoc
 Invalidtype71.11.162.253 - vty login ok - version - eth0 - admin - 1234
 Invalidtype71.11.162.254 - vty login ok - version - eth0 - admin - 1234
 Invalidtype10.173.73.1 - vty login failed
 Invalidtype
in my final output file
instead of

bla bla bla bla bla router type
bla bla bla bla bla router type

i get

bla bla bla bla bla
routertype bla bla bla bla
router type bla bla

and it shows invalid type because grep was not able to match. . please help me fix this code


and no need to fret.. these are PRIVATE ip addresses for which i have explicit permission to test this script on



Update

Code:
10.173.73.254 - vty login ok - version - eth0 - admin - 1234^M Invalidtype
10.169.247.34 - vty login ok - version - eth0 - tulipadmin - pl3p30tcnoc^M Invalidtype
71.11.162.253 - vty login ok - version - eth0 - admin - 1234^M Invalidtype
71.11.162.254 - vty login ok - version - eth0 - admin - 1234^M Invalidtype
10.173.73.1 - vty login failed^M Invalidtype
FINALRESULT (END)
this is just ^M chars which i will remove to get the final file so one issue is resolved.. now please help me match the grep patter so that it displays the correct device type

Last edited by threeonethree; 11-29-2010 at 06:27 AM.
 
Old 11-29-2010, 07:43 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by threeonethree View Post
output i get

./tacacs: line 35: conditional binary operator expected
./tacacs: line 35: syntax error near `--silent'
./tacacs: line 35: `if [[ grep --silent Cisco $file ]]; then'
Firstly the script you posted is not the one the error message came from. Secondly it is helpful to post line numbers (comments on key lines would be enough) when error messages include line numbers.

The done < "$HOME/Desktop/autotel/temp1/RESULT" switches stdin from the ls output of done <<< $(ls -1) so the next time while read file is executed it does not get the next file but the next line from $HOME/Desktop/autotel/temp1/RESULT.

This can be solved using something like (not tested):
Code:
cd "$temp" || exit 1
exec 3 < /home/noc/Desktop/autotel/temp1/RESULT # Set up file descriptor/unit 3 as input from the file
while read file
do
    while read -u3 line # Read from file descriptor/unit 3
    do
        if [[ grep --silent Cisco $file ]]; then
            echo "$line cisco"
        elif [[ grep --silent Maipu $file ]];then
            echo "$line maipu"
        elif [[ grep --silent V3.90 $file ]];then
            echo "$line zyxel3.9"
        else
            echo "$line Invalid Type"
        fi
  done >/home/noc/Desktop/autotel/temp1/FINALRESULT
done <<< $(ls -1)
exec <&3- # Close file descriptor/unit 3
 
Old 11-29-2010, 08:36 AM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Code:
if [[ grep --silent Cisco $file ]]; then
should be

Code:
if grep --silent Cisco $file; then
 
1 members found this post helpful.
Old 11-29-2010, 08:49 AM   #11
threeonethree
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Original Poster
Rep: Reputation: 0
easier

Quote:
noc@noc-desktop:~/Desktop/pingtest$ ls -1
1
2
3
scr
scr~
noc@noc-desktop:~/Desktop/pingtest$ ./scr
found nothing
Quote:
#! /bin/bash

cd "$HOME/Desktop/pingtest" || exit 1
while read line;
do
if grep --silent hello $file; then
echo "found hello in $line"
elif grep --silent apple $file; then
echo "found apple in $line"
elif grep --silent mango $file; then
echo "found mango in $line"
else
echo "found nothing"
fi
done <<< `ls -1`
man this took all day but still doesnt work..

1 2 and 3 files have apple mango hello respectively
 
Old 11-29-2010, 09:04 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ntubski View Post
Code:
if [[ grep --silent Cisco $file ]]; then
should be

Code:
if grep --silent Cisco $file; then
Oops! Thanks for picking that up ntubski
 
Old 11-29-2010, 09:15 AM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
@threeonethree: You never set file
Code:
while read line;
do 
    if grep --silent hello $file; then # did you mean $line there?
         echo "found hello in $line"
    elif ...
Also please use [CODE][/CODE] tags not [QUOTE][/QUOTE].
 
Old 11-29-2010, 09:20 AM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Scripts show better in CODE tags than QUOTE tags.

Errors in red:
Code:
while read file;
do
if grep --silent hello $file; then
echo "found hello in $file"
elif grep --silent apple $file; then
echo "found apple in $file"
elif grep --silent mango $file; then
echo "found mango in $file"
else
echo "found nothing"
fi
done <<< "`ls -1`"
Output from `<command>` (if you insist on using it in preference to the saner $(<command>) ) is parsed by the shell into IFS-character-separated words and re-presented as space-separated words so your loop only executes once, with $file set to 1 2 3 (and whatever other files are in the current working directory).

You could have discovered these problems by tracing the script. Scripts can be traced by bash -x <path to script> or by putting set -xv in the script before the problem section (and set +xv to turn it off). Alternatively by adding debug statements to the script:
Code:
#! /bin/bash

cd "$HOME/Desktop/pingtest" || exit 1
while read line;
do
    echo "DEBUG: \$line is '$line'"  # The single quotes make leading or trailing spaces apparent
    if grep --silent hello $file; then
        echo "found hello in $line"
    elif grep --silent apple $file; then
        echo "found apple in $line"
    elif grep --silent mango $file; then
        echo "found mango in $line"
    else
        echo "found nothing"
    fi
done <<< `ls -1`
 
  


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
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 09:41 AM
BASH Sort list by end of line to x position in each line? SilversleevesX Programming 14 08-19-2010 08:30 PM
bash line 74: syntax error: unexpected end of file help? andycol Linux - General 5 09-14-2009 08:12 AM
bash: append string to end of line khairil Programming 6 02-27-2007 05:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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