Cut text from a file and remove

Hello Friends,

I am stuck with the below problem.Any help will be appreciated.
I have a file which has say 100 lines.
On the second last line I have a line from which i want to remove certain characters..
e.g

CAST(CAST( A as varchar(50)) || ',' ||
CAST(CAST( B as varchar(50)) || ',' ||
CAST(CAST( C as varchar(50)) || ',' ||
CAST(CAST( C as varchar(50)) || ',' ||
FROM qwer 

Now from the second last line i want to remove

|| ',' ||

.How can that be done.
I can cut using ) i.e

cut -d ')' -f3  

which will give me || ',' || .Now I want to remove this from the file.
But this I want only for the second last line.I have managed to pick out the number of line where i want this to happen.But how can I remove the above from there.
How can I do this.

Please help.

Your specification is rather vague. You say you want to remove || ',' || (not including the space before it, but you say cut -d ')' -f3 will give you the text to be removed (which does include the space before it). What do you want to use as the pattern to match: parentheses or vertical bars.

You don't say if you want to modify the file in place or just copy the modified text to standard output.

If you want to remove zero or more spaces followed by two vertical bars followed by any string of characters followed by two vertical bars from the next to the last line and replace the original contents of your file with the change applied, you could try:

ed -s file <<-EOF
        \$-1s/ *||.*||//
        w
        q
EOF

If you want to copy the updated file to standard output and leave the original file unchanged, try:

ed -s file <<-EOF
        \$-1s/ *||.*||//
        1,\$p
        q
EOF

If neither of these meet your needs, please give a more precise definition of what you do want.

Hi,
Ok...to be precise my requirement is to simply remove the

|| ',' ||

from the second last line in a shell script.I want to do that in place.Just want to discard that part completely.
Hope I am understood.

OK. So, use this trivial modification to my first suggestion:

ed -s file <<-EOF
        \$-1s/||.*||//
        w
        q
EOF

Try

sed -n 'x; $s/ |.*//; 1!p; ${x;p}' file
CAST(CAST( A as varchar(50)) || ',' ||
CAST(CAST( B as varchar(50)) || ',' ||
CAST(CAST( C as varchar(50)) || ',' ||
CAST(CAST( C as varchar(50))
FROM qwer
1 Like

Hi RudiC,

Could you please explain the flags you have used ? So i can be sure.

-n - don't automatically print lines

x; - swap the current pattern space (in this case, the whole line just read) with the 'hold' buffer. When the first line is processed this swaps the 'A' line into 'hold' and has nothing in the pattern space, for the second line it swaps the 'B' line into 'hold and the 'A' line back into the pattern space, and so on.

$s/ |.*// - on processing the last line, replace (in the pattern space) ' |' followed by any number of any character with nothing. (Note that the replace is actually happening on the last-but-one line, since it was just swapped out of hold.)

1!p; - on processing any line except the first, print the pattern space (which is now the previous line).

${x;p} - on processing the last line, swap the hold & pattern space again (which gets the actual last line back) and print the pattern space.

Hopefully that makes sense. It's late :).

1 Like

This should work if there are multiple occurrences of that pattern in the file..

sed "N;/\n.*|| ',' ||/!s/|| ',' ||//;"'$!P;$!D' file

file:

CAST(CAST( A as varchar(50)) || ',' ||
CAST(CAST( B as varchar(50)) || ',' ||
CAST(CAST( C as varchar(50)) || ',' ||
CAST(CAST( C as varchar(50)) || ',' ||
FROM qwer
CAST(CAST( A as varchar(50)) || ',' ||
CAST(CAST( B as varchar(50)) || ',' ||
CAST(CAST( C as varchar(50)) || ',' ||
CAST(CAST( C as varchar(50)) || ',' ||
FROM qwer

Output:

CAST(CAST( A as varchar(50)) || ',' ||
CAST(CAST( B as varchar(50)) || ',' ||
CAST(CAST( C as varchar(50)) || ',' ||
CAST(CAST( C as varchar(50)) 
FROM qwer
CAST(CAST( A as varchar(50)) || ',' ||
CAST(CAST( B as varchar(50)) || ',' ||
CAST(CAST( C as varchar(50)) || ',' ||
CAST(CAST( C as varchar(50)) 
FROM qwer

CarloM: I couldn't have done better explaining.