extracting part of a line excluding particular word from it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting part of a line excluding particular word from it
# 1  
Old 11-30-2011
extracting part of a line excluding particular word from it

here is the line on which i want to process
Code:
`empNo` int(13) NOT NULL AUTO_INCREMENT,

it sometimes doesnt have comma at the end too

Code:
`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

Code:
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.... :-/
# 2  
Old 11-30-2011
vivek,

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

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

If this didn't worked out, paste some lines of sample input.
# 3  
Old 11-30-2011
oky,
the number of words in not fixed...

let say the input file is
Code:
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
Code:
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..

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

it satisfies only if case is same... is there anyway i could compare case insesitively..?
# 4  
Old 11-30-2011
sed one liner.

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

# 5  
Old 11-30-2011
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..?
# 6  
Old 11-30-2011
Quote:
Originally Posted by vivek d r
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".

Code:
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.

Code:
while read line; do
    #Do something here
done < output.txt

This User Gave Thanks to MacMonster For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help needed in excluding certain word patterns

Hi, I need help with following. I need to exclude words that match following patterns a. more than length 4 (example SBRAP) b. contains mixture uppercase and lower case regardless of the length (example GSpD) File contains COFpC MCHX SP SNFCA GEH SBRAP DGICA JPMpE WFCpP GSpD AXL... (5 Replies)
Discussion started by: jakSun8
5 Replies

2. UNIX for Dummies Questions & Answers

Extracting part of a word

I have the code message={TP=2012:09:23:00:00:00:GMT,SD=2012:09:23:00:00:00:GMT,SP=2,FT=CCGT,FG=3605} I want to extract the FG=3605 parts of this. Please help. I am trying to do this using awk or unix. (5 Replies)
Discussion started by: JenniferTopham
5 Replies

3. Shell Programming and Scripting

Help needed in excluding word

Hello, When i run this command find ./ -type f -print | xargs grep -l 'myWord' it prints the file name where 'myWord' exists and also something like the below which i want to exclude grep: can't open Fields grep: can't open Search I tried piping and grep -v 'open' but it did not work. ... (3 Replies)
Discussion started by: jakSun8
3 Replies

4. Shell Programming and Scripting

Help with extracting a part of a line between two patterns

Hello All, I have a text file with contents as below: contents of error.txt: message1="Reason for error code1" message2="Reason for error code2" message3="Reason for error code3. To solve this, you may try doing restart" I have a requirement where in I have to... (4 Replies)
Discussion started by: asterisk-ix_use
4 Replies

5. UNIX for Dummies Questions & Answers

How to get complete path in variable excluding last word?

Hello Experts, Can you please help me by providing a code which can give me the complete path except last word in a variable, the variable i can use anywhere else for my operation eg: if this below one is my path: ... (3 Replies)
Discussion started by: aks_1902
3 Replies

6. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

7. Shell Programming and Scripting

Print part of line excluding one column

Hi, I have data which is having '|' as delimiter and have lobfilename/locations in the data. Ex: 1200|name1|lobfilename.0.600|abcd 1201|name2|lobfilename.600.1300|abcd My requirement is to print part of the line till the lobfilename and write to a different file and also print the... (4 Replies)
Discussion started by: newb
4 Replies

8. Shell Programming and Scripting

Extracting part of line between two words

Hi, I have a file few hundred MB's with text like one below in single line. 20091117 abc xyg 20091117 def ghi 20091118 ppp ttt 20091118 zzz zzz xxx I need to extract part of line from 1st occurence of pattern 20091117 till first occurence of another pattern 20091118. I tried... (3 Replies)
Discussion started by: artistic94555
3 Replies

9. Shell Programming and Scripting

Word count while excluding

Hello, Is there a way to word count a file while excluding lines that have FRP in them? wc -l "filename"? Thanks in advance Gideon (4 Replies)
Discussion started by: blitzkreg6
4 Replies

10. Shell Programming and Scripting

BASH: Grepping/sedding/etc out part of a file... (from one word to 'blank' line)

I have a file that lists data about a system. It has a part that can look like: the errors I'm looking for with other errors: Alerts Password Incorrect Login Error Another Error Another Error 2 Other Info or, just the errors I need to parse for: Alerts Password Incorrect ... (9 Replies)
Discussion started by: elinenbe
9 Replies
Login or Register to Ask a Question