extracting part of a line excluding particular word from it

here is the line on which i want to process

`empNo` int(13) NOT NULL AUTO_INCREMENT,

it sometimes doesnt have comma at the end too

`empNo` int(13) NOT NULL AUTO_INCREMENT

i want to extract all except "AUTO_INCREMENT" not only this line i ,want the code to work on any line if it has AUTO_INCREMENT... i can do this using string manipulation like

len=${#line1}
char=${line1:$len-1:$len}
if [ "$char" = "," ]
then
               line11=${line1:0:$len-15}
else
               line11=${line1:0:$len-14}
fi
 

but this is lengthy so is there any one liners to do the above function.... :-/

vivek,

check the below one-liner,as far as the columns/words are constant this will work out

awk -F " " '{ print $1,$2,$3,$4 }'  <filename>

If this didn't worked out, paste some lines of sample input.

oky,
the number of words in not fixed...

let say the input file is

CREATE TABLE `Table22` (
`empNo` int(13) NOT NULL AUTO_INCREMENT,
`channels` enum('one','two','three') COLLATE utf8_unicode_ci NOT NULL,
`reserved6` varchar(255),
PRIMARY KEY (`empno`)
) ENGINE=InnoDB AUTO_INCREMENT=2000 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

the above lines are in a file...
i will be reading them in a while loop.. so it will be stored in a variable called $line
in the above condition it is 'empno' but lets say we have

id` int(11) NOT NULL DEFAULT '0' AUTO_INCREMENT

since the while loop is dynamic it should work for all formats...

---------- Post updated at 12:27 PM ---------- Previous update was at 12:17 PM ----------

i have one more doubt.. say i have some contents in two variables
$aaa and $bbb.... say $aaa has "vivek" and $bbb has "ViVeK"
so if i perform below..

if [ "$aaa" = "$bbb" ]

it satisfies only if case is same... is there anyway i could compare case insesitively..?

sed one liner.

sed -n 's/AUTO_INCREMENT[,]\?$//pI'

where is the input variable in the above line..? if the input line is stored in a variable called $line how to use it in the above statement... and also i want the output to be stored in another variable say $modified...
and also how to compare variables with case insensitive as i asked previously..?

Assume the input file is called "table2.sql".

sed -n 's/AUTO_INCREMENT[,]\?$//pI' < table2.sql > output.txt

It saves the output into a temp file called "output.txt". Then read it line by line with shell script and do your job in the loop.

while read line; do
    #Do something here
done < output.txt
1 Like