| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
speed test
My concerns about a shell while loop were correct. The while-do loop is
much slower.
1.079455912 vs. .221387482
while-loop script:
#!/bin/sh
START=$(date +%s.%N)
while IFS=',' read a b c d
do
c="$(echo "$c" | sed -r 's/(.*)/\L\1/; s/\<(.)/\u\1/g' )"
echo "$a ; $b ;$c ; $d"
done < $1 > $2
END=$(date +%s.%N)
echo "$END - $START" | bc
#endofscript
cut-paste script:
#!/bin/sh
START=$(date +%s.%N)
col_to_sed=3
prev_col=`expr $col_to_sed - 1`
next_col=`expr $col_to_sed + 1`
cut -d \; -f -$prev_col < $1 > cols_before.txt
cut -d \; -f $col_to_sed < $1 > cols_.txt
cut -d \; -f $next_col- < $1 > cols_after.txt
sed -e '1b;s/.*/\L&/;s/\<./\u&/g' < cols_.txt > cols_new.txt
paste -d\; cols_before.txt cols_new.txt cols_after.txt > output.csv
END=$(date +%s.%N)
echo "$END - $START" | bc
#endofscript
On 12/12/15 02:46, [email protected] [sed-users] wrote:
> Here are a couple of minor changes to this script:
>
> A. Add a semicolon to the end of the command, rather than let the newline
> act as the command separator. This is handy in case lines become joined,
> then the command logic goes haywire.
>
>
> > cut -d ";" -f $col_to_sed $input_file > old_col.txt
> cut -d ";" -f $col_to_sed $input_file > old_col.txt ;
>
>
> B. i)Reformat the 'cut' commands in the order of the columns cut.
> ii) The 1-$prev_col can be simplified to just -$prev_col.
> iii) Intermediate output filenames are made brace expansion friendly.
> iv) Input to "cut" command is redirected
>
>
> > cut -d ";" -f $col_to_sed $input_file > old_col.txt
> > cut -d ";" -f 1-$prev_col $input_file > cols_before.txt
>> cut -d ";" -f $next_col- $input_file > cols_after.txt
>
>
> cut -d \; -f -$prev_col < $input_file > cols_before.txt
>
> cut -d \; -f $col_to_sed < $input_file > cols_.txt
> cut -d \; -f $next_col- < $input_file > cols_after.txt
>
>
>
>
>
> C. The "sed" command is slightly altered to do away with the capturing
> parentheses.
>
>> sed -r "1! {s/(.*)/\L\1/; s/\<(.)/\u\1/g}" < old_col.txt > new_col.txt
>
>
> sed -e '1b;s/.*/\L&/;s/\<./\u&/g' < cols_.txt > cols_new.txt
>
>
>
>
>
> D. Finally, the "paste" command with the brace expansion:
>
>
> paste -d\; cols_{before,new,after}.txt
>
>
>
>
>
>
>
> ---In [email protected], <dgoldman@...> wrote :
>
> Thanks for sending the more complete file example. Here is one way to do
> it, in addition to the other suggestions.
>
> $ cat input.txt
> "id";"lastname";"firstname";"adress";"zip";"town"
> "9515";"CRAIG";"PAUL";"3 Av Marie Curie";"75001";"Paris"
> "355";"BROSNAN";"MARIE ODILE";"43 Rue PASTEUR";"75001";"Paris"
> "85";"CONNERY";"JEAN-CHARLES";"7 Rue LECOURBE";"14256";"Seville"
> "5315125";"MOORE";"JEAN-MARC";"12 Av PASTEUR";"47852";"Rome"
>
> $ cat process.sh
> input_file=$1
> col_to_sed=$2
>
> prev_col=`expr $col_to_sed - 1`
> next_col=`expr $col_to_sed + 1`
>
> cut -d ";" -f $col_to_sed $input_file > old_col.txt
> cut -d ";" -f 1-$prev_col $input_file > cols_before.txt
> cut -d ";" -f $next_col- $input_file > cols_after.txt
>
> sed -r "1! {s/(.*)/\L\1/; s/\<(.)/\u\1/g}" < old_col.txt > new_col.txt
>
> paste -d ";" cols_before.txt new_col.txt cols_after.txt
>
> $ ./process.sh input.txt 3
> "id";"lastname";"firstname";"adress";"zip";"town"
> "9515";"CRAIG";"Paul";"3 Av Marie Curie";"75001";"Paris"
> "355";"BROSNAN";"Marie Odile";"43 Rue PASTEUR";"75001";"Paris"
> "85";"CONNERY";"Jean-Charles";"7 Rue LECOURBE";"14256";"Seville"
> "5315125";"MOORE";"Jean-Marc";"12 Av PASTEUR";"47852";"Rome"
>
> On the negative side, the shell script might need to be modified if you
> have cases where col_to_sed is the first or last column, so the script
> would end up more complicated.
>
> On the positive side, it works, and (at least to me!) seems simple
> enough to understand and maintain.
>
> Daniel
>
> On 12/11/2015 12:34 AM, MOKRANI Rachid [sed-users] wrote:
> > Hi,
> >
> > Thanks, but this solution (without -i) show me the result and don't write/save the file.
> >
> > This is a file exemple. the original file has many more rows and columns
> >
> > "id";"lastname";"firstname";"adress";"zip";"town"
> > "9515";"CRAIG";"PAUL";"3 Av Marie Curie";"75001";"Paris"
> > "355";"BROSNAN";"MARIE ODILE";"43 Rue PASTEUR";"75001";"Paris"
> > "85";"CONNERY";"JEAN-CHARLES";"7 Rue LECOURBE";"14256";"Seville"
> > "5315125";"MOORE";"JEAN-MARC";"12 Av PASTEUR";"47852";"Rome"
> >
> >
> > in my example I would apply the results of sed (sed -i -r "s/(.*)/\L\1/; s/\<(.)/\u\1/g" ) only in column 3 (title firstname) .
> >
> > "id";"lastname";"firstname";"adress";"ZIP";"TOWN"
> > "9515";"CRAIG";"Paul";"3 Av Marie Curie";"75001";"Paris"
> > "355";"BROSNAN";"Marie Odile";"43 Rue PASTEUR";"75001";"Paris"
> > "85";"CONNERY";"Jean-Charles";"7 Rue LECOURBE";"14256";"Seville"
> > "5315125";"MOORE";"Jean-Marc";"12 Av PASTEUR";"47852";"Rome"
> >
> >
> > Once obtained this result I could apply a sed command on the column of my choice.
> >
> > And for now I just can't do it. :-(
> >
>
>
>
>
>
>
> [Non-text portions of this message have been removed]
>
>
>
> ------------------------------------
> Posted by: [email protected]
> ------------------------------------
>
[Non-text portions of this message have been removed]