LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-26-2012, 05:19 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
Bash: Is there a builtin command to write lines?


Hi:

The read command reads one line of text. Is there a builtin command to write lines? prinf write a line and I can use an empty format string. But it realy writes FORMATTED lines. In C there is puts, and all the other puts besides printf. So may be in bash there is something like it.

GNU bash, version 3.1.17

Last edited by stf92; 07-26-2012 at 05:21 AM.
 
Old 07-26-2012, 05:24 AM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Have a look at the echo command.
 
2 members found this post helpful.
Old 07-26-2012, 05:28 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thanks a lot. I would never had guess it.
 
Old 07-26-2012, 05:32 AM   #4
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Rep: Reputation: 26
Maybe you can have a look at the printf command (for BASH) too!
 
Old 07-26-2012, 06:03 AM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
By the way, consider
Code:
#!/bin/sh

while read line ;
do
echo $line
done<foo >jose
If for each line that is read I want to output it, how do I do? Modifying the echo line, that is 'echo $line > some_file won't do.
 
Old 07-26-2012, 06:18 AM   #6
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Quote:
Originally Posted by stf92 View Post
By the way, consider
Code:
#!/bin/sh

while read line ;
do
echo $line
done<foo >jose
If for each line that is read I want to output it, how do I do? Modifying the echo line, that is 'echo $line > some_file won't do.
Code:
cp foo jose

Jokes aside, do you want to append every single line read inside the while cycle to the same output file? You should use the ">>" operator instead of the ">", which overwrites every time the output file.
 
1 members found this post helpful.
Old 07-26-2012, 06:18 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Or:
Code:
while read line ;
do
    printf "%s\n" "$line"
done<foo >jose
 
Old 07-26-2012, 06:22 AM   #8
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thanks. I did it the other way:
Code:
echo $line >> jose
But I tried your solution, only that with only one '>' in the echo sentence.
 
Old 07-26-2012, 06:30 AM   #9
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Quote:
Originally Posted by 414N View Post
Code:
cp foo jose

Jokes aside, do you want to append every single line read inside the while cycle to the same output file? You should use the ">>" operator instead of the ">", which overwrites every time the output file.
Its only a skeleton for aa larger script. And is it possible to locate a given substring within line with only buitins? And with an external command?

Last edited by stf92; 07-26-2012 at 06:33 AM.
 
Old 07-26-2012, 06:43 AM   #10
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
You can use the case statement, which can apply some kind of pattern matching, like:
Code:
case $VAR in
foo?foo)
	echo "fooXfoo found"
	;;
*bar*)
	echo "bar found inside the string"
	;;
esac
If case can't help you, you can try grep, sed or awk.
 
1 members found this post helpful.
Old 07-26-2012, 06:49 AM   #11
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Case is then all I need. Thanks a lot.
 
Old 07-26-2012, 06:52 AM   #12
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
You're welcome
Please mark the thread as solved if the original issue is no more.
 
Old 07-26-2012, 07:09 AM   #13
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
I do not understand the case syntax. I want to do (pseudocode)
Code:
if (PERFORMER in $line) then
{
continue
else
echo $line
}
How should I do it with case? Is there an else case in the case construction?

All's well that ends well. Now I understand it.

Last edited by stf92; 07-26-2012 at 07:37 AM.
 
Old 07-26-2012, 07:18 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
grep -v -- "PERFOMER" <inputfile | while read line; do
done
 
Old 07-26-2012, 07:22 AM   #15
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Is PERFORMER the literal substring you're searching in every line?
If so:
Code:
case $line in
*PERFORMER*)
	continue
	;;
*)
	echo $line
	;;
esac
The patterns you can specify in a case statement (those before the 'close bracket' ')') can use wildcards such as * (0 or more characters) or ? (0 or 1 character) or + (1 or more characters).
In this case, we first look if $line contains PERFORMER in a non-specified position (* before and after it means that there can be 0 or more chars before and after it) and, if so, we continue.
If there was no match with the first pattern, than case tries to match $line with the second one (which matches everything) and proceeds to echo the line.
Hope this is clear enough to understand.
 
  


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
Writing a file character by character with a bash builtin command (script). stf92 Linux - Newbie 4 06-30-2012 08:41 PM
[SOLVED] Bash command to turn multiple lines to one line medirecpr Linux - Newbie 5 05-02-2012 06:39 AM
[SOLVED] How write a literal bash command in a bash file? xeon123 Linux - Newbie 6 11-29-2010 12:05 PM
[SOLVED] bash invocation question, using 'history' builtin. bartonski Programming 9 10-18-2009 09:00 PM
complex command lines in bash? 3inone Linux - Newbie 1 04-20-2004 06:43 PM

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

All times are GMT -5. The time now is 03:12 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