LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-16-2014, 12:07 PM   #1
sandeep_hello
Member
 
Registered: Feb 2008
Posts: 62

Rep: Reputation: 1
Script to print line


Hi,

i have one file for data


# cat file1
a b c
d e f
g h i

i want to print this line by line. I am writing script like this.

#!/bin/bash

TXT=/root/user

for i in `cat $TXT`
do
echo $i
done


but it is printing line this

a
b
c
d
e
f
g
h
i


output should come like this

a b c

d e f

g h i

Please suggest me the best way.


Regards
Sandeep Sharma
 
Old 01-16-2014, 12:15 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
cat file1 |while read line ; do echo $line; done
 
1 members found this post helpful.
Old 01-16-2014, 10:43 PM   #3
sandeep_hello
Member
 
Registered: Feb 2008
Posts: 62

Original Poster
Rep: Reputation: 1
Thank for your reply.

actually i want to use for loop for further processing. I will parse each line and store in a variable but this is not happening in my script.


Like

Line by line

First=a
second=b
third=c

First=d
second=e
third=f

First=g
second=h
third=i


Please suggest on this.

Regards
Sandeep Sharma
 
Old 01-16-2014, 11:16 PM   #4
mutput7
LQ Newbie
 
Registered: Oct 2012
Location: A magical place full of Canadian dirt.
Distribution: Arch Linux
Posts: 12

Rep: Reputation: 4
I'm not sure what you mean, but you might want to try defining a function so you can more easily parse the lines later:
Code:
function parseline {
  line=$@
  echo $line
}
while read line ; do parseline $line; done < file1

Last edited by mutput7; 01-16-2014 at 11:52 PM. Reason: evo2 pointed out how to spare some cat usage
 
Old 01-16-2014, 11:39 PM   #5
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

avoiding useless use of cat:
Code:
while read l ; do
  echo "The line is $l"
done < file1
HTH,

Evo2.
 
Old 01-17-2014, 12:20 AM   #6
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
@sandeep :
you already had 2 nice solutions, if you still want it in for*, take a a look on how while is used and try on your own first, we will help if you stuck.
 
  


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
read a line from file , parse it and store in variables,print them using shell script swapnika.a60 Linux - Newbie 2 12-20-2012 07:20 AM
Print output in a single line in perl script Venkadesh Linux - Newbie 4 01-28-2012 11:52 AM
How to compare two files line by line and print the line which is same. nancypriyanjali Programming 9 05-30-2011 10:04 PM
Print all PID folders from /proc line-by-line with this format (( PID: command-line )) courteous Linux - Newbie 7 12-12-2010 04:47 PM
command or shell script to print line range from file minil Programming 3 12-28-2005 08:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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