LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Editing Every 10th Line of a File (https://www.linuxquestions.org/questions/linux-newbie-8/editing-every-10th-line-of-a-file-921843/)

linux26f 01-03-2012 02:58 PM

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.

TB0ne 01-03-2012 03:26 PM

Quote:

Originally Posted by linux26f (Post 4565041)
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.

makyo 01-03-2012 03:49 PM

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" )

lucmove 01-03-2012 03:53 PM

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

Cedrik 01-03-2012 07:58 PM

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


All times are GMT -5. The time now is 10:35 AM.