LinuxQuestions.org
Review your favorite Linux distribution.
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 04-05-2018, 10:54 AM   #1
alxreds
LQ Newbie
 
Registered: Dec 2017
Posts: 8

Rep: Reputation: Disabled
Question Copy Line in File


Hello
I have the file with

apple1
orange1
lime1

apple1
orange1
lime1

i need with

apple1
orange1
orange2
lime1

apple1
orange1
orange2
lime1

I Try with
sed '2,$ s/[^,]*$/&,&/' file | sed -r ':a;N;s/\n([^,]*)(,?[^,]*)(,[^,]*)$/\3\n\1\2/;ta;' | sed '$d'

Some one have any idea ?
Thanks
 
Old 04-05-2018, 11:48 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,440

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
"Copy Line in File" is too vague to give an answer even. what line are you trying to copy?
 
Old 04-05-2018, 01:10 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,226

Rep: Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709
Also what are these [^,] for while there is no ',' in source file?
 
Old 04-05-2018, 01:28 PM   #4
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 1,968

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
Code:
echo -e "apple1\norange1\nlime1" > file1.txt

sed '/orange1/a orange2' file1.txt
apple1
orange1
orange2
lime1
 
Old 04-05-2018, 04:00 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,226

Rep: Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709
I don't know how to count with sed...

Input:
Code:
apple1
orange1
lime1

apple1
orange1
lime1

apple1
orange2
lime1

apple1
orange125
lime1
Perl code:
Code:
perl -pe '$.==($a+2) and s/(.*)(\d+)$/"$&\n$1".($2+1)/e, $a+=4' file
Output:
Code:
apple1
orange1
orange2
lime1

apple1
orange1
orange2
lime1

apple1
orange2
orange3
lime1

apple1
orange125
orange126
lime1
Not sure if I got the logic though
 
  


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
How to copy one line from one file to another file on line number.. gajananh999 Linux - Newbie 1 08-13-2012 05:27 AM
C - Copy File Line By Line With Some Slight Changes golmschenk Programming 11 03-03-2010 01:46 PM
Copy line from file and write it to another file shokeer Linux - Newbie 2 11-01-2008 06:54 AM
how copy a file from a cd into the computer using the comand line? mariav Linux - Software 3 04-13-2007 10:07 PM
command line CDR / copy file rioguia Linux - Newbie 4 10-05-2002 04:52 AM

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

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