Cutting a part of line till delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cutting a part of line till delimiter
# 1  
Old 12-30-2011
Cutting a part of line till delimiter

here are the few scenarios...

Code:
isoSizeKB text NOT NULL,
 reserved1 varchar(255),
      KEY `deviceId` (`deviceId`)
`d5` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`dHead` enum('HistoryInfo','Diversion') COLLATE utf8_unicode_ci,
`ePR` int(11) DEFAULT '0',
  PRIMARY KEY (`id`)

what i want to achieve is if the line has ',' at the end it deletes it... and also spaces(or tabs) before or after the line....

i tried echo "reserved1 varchar(255)," | cut -d ',' -f1 | sed 's/^[ ]*//g;s/[ ]*$//g'
but this willl not work for all conditions say if there is , inbetweeen the line...

---------- Post updated at 02:38 PM ---------- Previous update was at 02:07 PM ----------

i was using
Code:
linexT=$( echo $linexT  | sed 's/^[ ]*//g;s/[ ]*$//g' )
                                                 len=${#linexT}
                                                echo "len: $len"
                                                 charlast=${linexT:$len-1:$len}
                                                  if [[ "$charlast" = "," ]]
                                                 then
                                                        line11=${linexT:0:$len-1}
                                                 else
                                                      line11=$linexT
                                                 fi

the above since many days but the above doesnt work for few scenarios.. so is there any one liner for this.. which works for all the scenarios mentioned in my previous post?
# 2  
Old 12-30-2011
Code:
 perl -lne 's/^\s*|\s*$//;print unless /,$/' infile

# 3  
Old 12-30-2011
ur code will work fine for spaces not for tabs, can u give us some i/p, expected and obtained o/p.
# 4  
Old 12-30-2011
Hi vivek d r,

Try if this could work for you. It deletes all lines which end with ',' and for those which not, deletes dealing and trailing spaces:
Code:
$ perl -ne 'unless ( /,\s*$/ ) { s/^\s*//; s/\s*$//; printf qq[%s\n], $_ }' infile
KEY `deviceId` (`deviceId`)
PRIMARY KEY (`id`)

Regards,
Birei
# 5  
Old 12-30-2011
consider the content of the variable $line11 is any of the below
Code:
line11="isoSizeKB text NOT NULL,"
similarly below ones....
 reserved1 varchar(255),
or
      KEY `deviceId` (`deviceId`)
or
`d5` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
or
`dHead` enum('HistoryInfo','Diversion') COLLATE utf8_unicode_ci,
or
`ePR` int(11) DEFAULT '0',
or
  PRIMARY KEY (`id`)

expected output is
Code:
isoSizeKB text NOT NULL
 reserved1 varchar(255)
KEY `deviceId` (`deviceId`)
`d5` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL
`dHead` enum('HistoryInfo','Diversion') COLLATE utf8_unicode_ci
`ePR` int(11) DEFAULT '0'
  PRIMARY KEY (`id`)

respectively...
that is it should delte ',' (comma) present at the end of the line if present else not.. also it should delete empty spaces before the line (either n spaces or tabs)
# 6  
Old 12-30-2011
in ur snippet change as below
Code:
echo $linexT  | sed 's/^[ \t]*//g;s/[ \t]*$//g'

if u need one liner
Code:
echo $linexT  | sed 's/^[ \t]*//g;s/[ \t]*$//g;s/,$//g'

else
Code:
sed 's/^[ \t]*//g;s/[ \t]*$//g;s/,$//g' input_file

Try these...
This User Gave Thanks to siva shankar For This Post:
# 7  
Old 12-30-2011
i dont want to input any file to the command... the lines will be stored in a variable say $line11 i want to modify it then and there itself.. or modify it and store it in another variable

---------- Post updated at 03:22 PM ---------- Previous update was at 03:18 PM ----------

thanks shiva_shankar.. your code works for me :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cutting values with delimiter

Hi All, I have a string with , delimiter america,finland,netherlands Now i want these values to be stored in file as below with newline character at end of each value america finland netherlands Regards Prasad (3 Replies)
Discussion started by: krishna_gnv
3 Replies

2. Shell Programming and Scripting

Cutting a string using more than one character as delimiter

Hi , I have a set of files in a folder which i need to cut in to two parts.... Sample files touch AE_JUNFOR_2014_MTD_2013-05-30-03-30-02.TXT touch AE_JUNFOR_2014_YTD_2013-05-30-03-30-02.TXT touch temp_AE_JUNFOR_2014_MTD_2013-05-30-03-30-02.TXT touch... (4 Replies)
Discussion started by: chillblue
4 Replies

3. Shell Programming and Scripting

Any shell scripts for cutting and pasting part of data?

Hi, I have a tab-delimited txt file as below. It is part of the original file. I want to cut the lines starting with "3" in column1 and paste them before the lines starting with "1" in column 1. So I will get Anyone knows any simple shell scripts to do that? The original file is... (5 Replies)
Discussion started by: cliffyiu
5 Replies

4. Shell Programming and Scripting

Cutting Part of Output

Hello all I'm using bourne shell and need to figure out how to cut out a specific portion of some output. For example, my output from my command is: 12.12.52.125.in-addr.arpa name = hostname.domain.main.gov I need to get just the "hostname.domain.main.gov" part. What I'm trying... (9 Replies)
Discussion started by: lee.n.doan
9 Replies

5. UNIX for Dummies Questions & Answers

cutting columns if delimiter has more than one charecter (|^)

Hi All, I am having a file with the delimiter '|^'. File name:test_dlim.csv I want to cut the first field of this using awk command. I tried with the help of the following link:... (2 Replies)
Discussion started by: boopathyvasagam
2 Replies

6. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

7. UNIX for Dummies Questions & Answers

Cutting a portion of a line seperated by pipe delimiter

Hi, In the below line a|b|10065353|tefe|rhraqs|135364|5347575 dgd|rg|4333|fhra|grhrt|46423|urdsgd Here i want to cut the characters in between the second and third pipe delimiter and then between fifth and sixth delimiter and retain the rest of the line. My output should be ... (3 Replies)
Discussion started by: ragavhere
3 Replies

8. Shell Programming and Scripting

Cutting a tab delimiter file

I have a 30 column tab delimited record file. I need to extract the first 10column. The following command to cut was not working cut -f 1-10 -d "\t" filename. Could any one keep on this . Thanks in Advance (4 Replies)
Discussion started by: vinod.thayil
4 Replies

9. UNIX for Dummies Questions & Answers

cutting columns if delimiter has more than one charecter

Hi, My file looks like abc$%sdfhs$%sdf$%sdfaf$% here as seen delimiter is $%...now how cas i take out second field as cut command expect delimiter as single charecter only.....is there is any other way thanks and regards mahabunta (9 Replies)
Discussion started by: mahabunta
9 Replies

10. Shell Programming and Scripting

cutting part of string

Hi, I wanted to cut a specific portion from given string. How would I do that? Example: /u09/core/inbound/abc.txt is my string. I want abc.txt in a variable. Please help me. Regards, Dhaval (3 Replies)
Discussion started by: dhaval_khamar
3 Replies
Login or Register to Ask a Question