LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-22-2012, 07:04 PM   #1
captainentropy
Member
 
Registered: Mar 2010
Location: Berkeley
Distribution: Ubuntu, Mint, CentOS
Posts: 81

Rep: Reputation: 0
repeating lines X number of times based on variable using awk(?)


Hi,

I'm not sure if this is possible with awk. I don't know enough perl/python to approach this. It seems doable with awk but I don't know where to begin.

I have a file in this format:

Code:
5	apple	A
3	pear	B
1	banana	C
2	orange	D
I need to repeat the lines based on the variable in the first column so that the above data looks like this:

Code:
apple	A
apple	A
apple	A
apple	A
apple	A
pear	B
pear	B
pear	B
banana	C
orange	D
orange	D
Any ideas?
 
Old 06-23-2012, 04:17 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,505

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
Probably need to know more about how the remaining data needs to be formatted, but without this:
Code:
awk '{for(i=1;i<=$1;i++)print $2,$3}' OFS="\t" file
 
Old 06-25-2012, 02:19 PM   #3
captainentropy
Member
 
Registered: Mar 2010
Location: Berkeley
Distribution: Ubuntu, Mint, CentOS
Posts: 81

Original Poster
Rep: Reputation: 0
Thanks grail! This worked perfectly. No extra formatting needed. It seems like everything I need awk and/or sed can do it.
 
  


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
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
Generate alphanumeric characterlist - no repeating more then 3 times jumpingj Programming 2 04-06-2011 04:55 AM
[SOLVED] sed or awk question, find lines with signs and number them Krzysztow Linux - Newbie 14 05-18-2010 10:23 AM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
variable stored with times on seperate lines i need to add them up shell scripting xskycamefalling Programming 4 05-16-2009 02:11 AM


All times are GMT -5. The time now is 04:21 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration