LinuxQuestions.org
Review your favorite Linux distribution.
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 10-04-2012, 11:49 AM   #1
beaven67
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Rep: Reputation: 0
Bash shell script with for loop question?


Ive got some data in a file in the following format.
1.2.3.4
5.5.5.5
5.6.7.8
5.5.6.5
and I need it presented and or written to a separate file like
1.2.3.4 5.5.5.5
5.6.7.8 5.5.6.5

Ive tried using a seperate for loop but I must be missing something? This is probably an easy one!
Thanks,
 
Old 10-04-2012, 12:03 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hi,

we don't do your homework for you. Please post the code you've written so far, then we can help you.

Markus
 
Old 10-04-2012, 01:15 PM   #3
beaven67
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Original Poster
Rep: Reputation: 0
Here you go..
for k in echo `cat half-formatted.txt`
do
for j in echo `cat half-formatted.txt`
do
echo $k $j
done
done
thanks
 
Old 10-04-2012, 03:14 PM   #4
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
...and just after saying we wouldn't do his homework for him.

But in any case the above is really incorrect (as is the thread title), because we Don't Read Lines With For! File processing should always be done with a while+read loop.


Also here's a hint for you. When you need to work on every nth something-or-other, you should generally make use of the modulo operator (%). It outputs the remainder after a division operation. Example:

Code:
if (( a % 2 == 0 )); then
	echo "the number is even"	#e.g. 8/2 has a remainder of zero
else
	echo "the number is odd"	#e.g. 9/2 has a remainder of one
fi
Now just put a counter in your loop to test. Save the line on every odd count, and concatenate the next line to it on every even count and print it.


(Actually, what I would probably do is simply read all the lines into an array with the mapfile command (assuming bash), then use printf to print them back out two at a time. Saves a lot of headache.)
 
1 members found this post helpful.
Old 10-04-2012, 03:24 PM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by David the H. View Post
...and just after saying we wouldn't do his homework for him.
It was the OP who posted the code, posting what he'd done so far...

However, your comments are just as applicable.

Regards,
 
Old 10-04-2012, 03:59 PM   #6
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
Ooooops! Time for me to smack myself then.

I've been up too long. Time for bed.
 
Old 10-05-2012, 09:57 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
You could also use a simple awk for this. Once you have the bash portion done I am happy to post one for you
 
  


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
Need help with bash shell script for loop Thaidog Programming 6 03-28-2012 08:59 AM
Bash Script Loop Question SoulShaker Linux - Server 5 06-17-2009 01:44 PM
shell script question $variable in loop icecubeflower Linux - Newbie 2 03-31-2009 09:09 AM
Bash script Question while loop glennph93 Programming 6 05-25-2007 03:27 PM
bash script - for loop question rignes Programming 3 10-05-2004 11:16 PM

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

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