LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-08-2013, 10:16 AM   #1
Find
Member
 
Registered: Jan 2012
Posts: 32

Rep: Reputation: Disabled
Insert X after every Y sequences in file


How do I insert "X" after every #Y sequences in file? I want to use sed or grep or some ubiquitous *inx software.

For example:
File =
Code:
`abcd,efg,hi ,jkl,  m`
X = (backspace and then a dollar symbol then newline)
Code:
`\b$\n`
Sequence = (anything and then a comma - but only anything once - eg. not-greedy )
Code:
`*,`
Y =
Code:
`2`
New File =
Code:
`abcd,efg$
hi ,jkl$
 m`

Thanks!
 
Old 01-08-2013, 08:15 PM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
Persoanlly, I would approach this by a bash script.

This is some outline pseudo code

Code:
IFS=","
while read -a aline
  for=1 to sizeof aline; do
    if (i modulo Y not equal to zero) AND (i less than sizeof aline)
      echo -n aline[i-1]","
    elif (i equals sizeof aline)
      echo aline[i]
    else
      echo aline[i-1]"$"
    fi
  done
done << inputfile
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Extract a column from file 1 and insert in file 2 ksvinaykumar Linux - Newbie 2 04-10-2012 06:20 AM
insert lines into a file after calculating where to insert xonar Programming 13 12-24-2009 04:37 AM
script to insert no. of lines in a file to the start of the file LarsB Linux - Newbie 3 09-17-2009 08:22 PM
using sed to insert line into file and overwrite the current file jadeddog Programming 3 06-11-2009 07:14 PM
tough one: how do you find patterns/sequences in file names? BrianK Programming 5 10-18-2007 12:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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