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 01-03-2012, 02:58 PM   #1
linux26f
LQ Newbie
 
Registered: Jan 2012
Posts: 1

Rep: Reputation: Disabled
Editing Every 10th Line of a File


Hi,

I am trying to replace the first word of every 10th line in a file. What would be the most efficient language to do this.

Thanks.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-03-2012, 03:26 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,807

Rep: Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179Reputation: 4179
Quote:
Originally Posted by linux26f View Post
Hi,
I am trying to replace the first word of every 10th line in a file. What would be the most efficient language to do this.
Thanks.
No idea, without some context. How big is the file? What kind of replacement are you talking about? (regex type string, or static?) Variable file names each time, or the same?

If you mean just a small-ish text file, replacing something like "string1" with "string2", bash would work fine. If you're talking about a 20GB file, and replacing strings like "Item-A13802-CL" with "ItemBCL13802", that's more of a job for Perl, or another more heavy-duty language.
 
Old 01-03-2012, 03:49 PM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 729

Rep: Reputation: 75
Hi.

GNU sed has a feature that allows firstline-step addresses, like so:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate first~step address selection for GNU sed.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C sed

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
sed -r '2~3s/^\w+/changed to this string/' $FILE

exit 0
producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39
GNU sed version 4.1.5

-----
 Input data file data1:
Bruckner kinetic cordon cryptography bantam calendrical
venture antagonistic. Drool adsalsey barbarism
horseshoe pappy acrylate. Dayton Friedrich inn maxima
capita sract vicar accordant. Sinai forgettable
ointment Hippocrates swampy hotelman upturn Kobayashi.
Kingsbury chorus gracte David Marty slavish!
Ophthalmology whixalvy int look fraught. Klelengy
ajectalk Bausch ordnance sturgeon hike Renault Kruse
prep gases potassium. Gallon carefree libation Ronnie
plasmon crust barbudo irchotche.

-----
 Results:
Bruckner kinetic cordon cryptography bantam calendrical
changed to this string antagonistic. Drool adsalsey barbarism
horseshoe pappy acrylate. Dayton Friedrich inn maxima
capita sract vicar accordant. Sinai forgettable
changed to this string Hippocrates swampy hotelman upturn Kobayashi.
Kingsbury chorus gracte David Marty slavish!
Ophthalmology whixalvy int look fraught. Klelengy
changed to this string Bausch ordnance sturgeon hike Renault Kruse
prep gases potassium. Gallon carefree libation Ronnie
plasmon crust barbudo irchotche.
Note that the separator is a tilde, not a dash. See GNU man sed for details ... cheers, makyo

( edit 1: forgot option "-r" )

Last edited by makyo; 01-03-2012 at 04:35 PM.
 
Old 01-03-2012, 03:53 PM   #4
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian Jessie
Posts: 755

Rep: Reputation: 73
1) Make sure you think of a string that is NOT present in the file. For example, "WOOTWOOT".

2) Add line number and "WOOTWOOT" before every line:
Code:
% nl -s " WOOTWOOT " file.txt > tempfile1.txt
Check tempfile1.txt and you will understand:
Code:
% less tempfile1.txt
3) Now the line number of every TENTH line MUST end in "0" (zero), therefore you can safely replace whatever you want in every line that contains "some number, ZERO, space, WOOTWOOT and space". That's a perfect job for sed:
Code:
% sed 's/[0-9]*0 WOOTWOOT /s/find this string/replace with this string/' tempfile1.txt > tempfile2.txt
4) Now let's clean the line numbers and WOOTWOOTs of the resulting file:
Code:
% sed 's/^.*WOOTWOOT //' tempfile2.txt > tempfile1.txt
5) Now check tempfile1.txt, it must be just the way you want it. If you're happy with the result, use it:
Code:
% mv tempfile1.txt file.txt
 
Old 01-03-2012, 07:58 PM   #5
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Code:
perl -i.bak -pe '$_=~s/\s*(\w+)/Word Replacement/ if $.=~/0$/' file
file updated in place and original saved as file.bak

(edit)
added \s just in case there are some space chars before the first word

Last edited by Cedrik; 01-03-2012 at 08:03 PM.
 
2 members found this post helpful.
  


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
Command line text file viewing and editing - .odt etc. mitchellray Linux - Newbie 1 04-14-2009 03:41 PM
Reading and printing the 5th, 10th, 15th...word of any file dahr Linux - Newbie 2 07-28-2008 03:51 PM
editing a file in command line during ssh session...need advise! reddog1 SUSE / openSUSE 8 05-31-2006 09:09 PM
PERL editing single line in file with regex indiescene Programming 0 04-14-2004 08:18 AM


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