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, 08: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, 05:17 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,030

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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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