cut command issue from a line of text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cut command issue from a line of text
# 1  
Old 12-02-2010
cut command issue from a line of text

Hi,
I got a line of text which has spaces in between and it is a long stream of characters. I want to extract the text from certain position. Below is the line and I want to take out 3 characters from 86 to 88 character position. In this line space is also a character. However when using cut command it excludes the spaces and I got wrong result.
Code:
LINE="AX80633030PHLIAD20209 1010104711411153             0054          000050000      11  PHLIAD1013112320100134-0003+003007000103+0033ER4ER4037012 N835HK0698D7   A2           A2E     05201011302010113020101130201011300000500000240000000TETE20101130NN8063201011300925IAD0004800045     5"
STR1=`echo $LINE | cut -c 86-88`
echo $STR1

It simply does not work. Can you pl help me with any alternative solution ?

Regards
Asutosh
# 2  
Old 12-02-2010
You don't need to run a whole command to get a substring, the shell has a builtin for it. This will be roughly one hundred times faster.

Code:
LINE="AX80633030PHLIAD20209 1010104711411153             0054          000050000      11  PHLIAD1013112320100134-0003+003007000103+0033ER4ER4037012 N835HK0698D7   A2           A2E     05201011302010113020101130201011300000500000240000000TETE20101130NN8063201011300925IAD0004800045     5"
echo "${LINE:86:3}"

...however, the data you want does not appear to be at position 86 anyway. (If the line has tabs in it, this may be a source of confusion since those would post as spaces on this board.) What do you actually want from this line?
# 3  
Old 12-02-2010
LINE="AX80633030PHLIAD20209 1010104711411153 0054 000050000 11 PHLIAD1013112320100134-0003+003007000103+0033ER4ER4037012 N835HK0698D7 A2 A2E 05201011302010113020101130201011300000500000240000000TETE20101130NN8063201011300925IAD0004800045 5"
echo $LINE|awk '{print substr($0,86,3)}'
# 4  
Old 12-20-2010
No, they do not work. I am getting error with the code echo "${LINE:86:3}". It says "${LINE:89:3}": The specified substitution is not valid for this command.

However the awk is working and giving me wrong result. There is no tab character - all spaces.
Thanks
Asutoshch
# 5  
Old 12-20-2010
awk's default delimiter is space and tab. So above awk should work.
A possible correction could be using double quotes around the LINE variable:
Code:
echo "$LINE"|awk '{print substr($0,86,3)}'

If it doesn't work, Pls post input and output data OR whatever result you get (And expected out)

Last edited by anurag.singh; 12-21-2010 at 06:39 AM.. Reason: typo
# 6  
Old 12-20-2010
@asutoshch
When you have a Shell problem, please state what Operating System you are running and what Shell you are using.
If you have an expected output, please tell us what you expect as the output.


Anyway based on your code, this should work in most circumstances. Always put double quotes round string variables whether it is necessary or not.

LINE="AX80633030PHLIAD20209 1010104711411153 0054 000050000 11 PHLIAD1013112320100134-0003+003007000103+0033ER4ER4037012 N835HK0698D7 A2 A2E 05201011302010113020101130201011300000500000240000000TETE20101130NN8063201011300925IAD0004800045 5"
STR1=`echo "$LINE" | cut -c 86-88`
echo "$STR1"


003

Last edited by methyl; 12-20-2010 at 06:18 PM.. Reason: typos
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with CUT command

Hi All, I am facing an issue while cutting fields with comma delimiter. There are many records with Double quotes in between. My data: aaa,bbb,"ccc, ddd",eee,fff,"ggg ,hhh",iii When i use cut command with comma delimiter, i am getting wrong data like field1=aaa field2=bbb field3="ccc... (3 Replies)
Discussion started by: krishna_gnv
3 Replies

2. Shell Programming and Scripting

Cut command issue

Need help to append a pipe at end of the line immediately after the cut command, I have an Input flat file with 16 feilds and I am removing the 16th feild by using the cut command as shown, Input: 354|||||CORPORTATION||||NENE PARADE|||WISBECH|CAMBRIDGESHIRE|PE13 3BY|100001| I... (5 Replies)
Discussion started by: Aditya_001
5 Replies

3. Shell Programming and Scripting

Cut command issue with shell script

I am writing a shell script to skip couple of feilds in a flat file and as a part of this I have written the below piece of code in it. cut -d '|' -f 1-17,19-31 $1 > filename To add pipe at end of the line I used below command, but it adds even to header and footer as well which i... (11 Replies)
Discussion started by: Aditya_001
11 Replies

4. Shell Programming and Scripting

CUT Command and grep issue

I am trying to grep the oracle erros evry day from the logs file. My problem is : -rw-r----- 1 tibcolm tibco 17438361 Apr 5 11:59 RetryService-RetryService.log -rw-r----- 1 tibcolm tibco 245303 Apr 5 12:00 ResponseService-ResponseService.log -rw-r----- 1 tibcolm tibco 2122654 Apr 5 12:00... (4 Replies)
Discussion started by: neeraj617
4 Replies

5. Shell Programming and Scripting

Extracting text using cut command

Hi All, I need to extract text 'ULTIMATE and 4spaces after it' from below line 77"2686"2010-11-21 14:09:13.000"4"I"200"1"2010-11-21 14:09:14.000"001:S087110131 ULTIMATE 4.99 T"" I have used cut command : a=`echo $line | cut -f9 -d '"' | cut -c20-31` echo $a But... (5 Replies)
Discussion started by: angie1234
5 Replies

6. Shell Programming and Scripting

cut the second line in a text file

Hi I have some problem to cut out the second line in a output file and send to a new file it's a #!/bin/bash script 1 something 2 something 3 something and after I cut 1 something 3 something New file 2 something Thanks in advance (7 Replies)
Discussion started by: pelle
7 Replies

7. Shell Programming and Scripting

Help need to cut the first word of a line in text file

Hi All, I would like help with a script which can get rid of the first work of all lines in text file. File 1 The name is Scott. Output : name is Scott ---------- Post updated at 02:38 PM ---------- Previous update was at 02:37 PM ---------- Hi ALL There is typo error in... (3 Replies)
Discussion started by: bubbly
3 Replies

8. Shell Programming and Scripting

How to cut first line only from a text near a specific column without cutting a word

First I have to say thank you to this community and this forum. You helped me very much builing several useful scripts. Now, I can't get a solution the following problem, I'm stuck somehow. Maybe someone has an idea. In short, I dump a site via lynx and pipe the output in a file. I need to... (7 Replies)
Discussion started by: lowmaster
7 Replies

9. Shell Programming and Scripting

how to cut a field of a line in a text file?

Hi, I have text file which contains lines like : a/a/a/a/.project b/b/b/b/b/.project c/c/c/.project d/.project e/e/e/e/.project i want for all lines the last word .project should be removed and the file should look like : a/a/a/a/ b/b/b/b/b/ c/c/c/ .... how to proceed... (7 Replies)
Discussion started by: bhaskar_m
7 Replies

10. Shell Programming and Scripting

Text cut between two $ in a line with SED

Let's say I have a line like that: I want to cut out numbers between two $ including $s. The result should be like that: I am so-newbei. I am non-stop reading about SED since yesterday and not a programmer. I know that it is a short period, thus maybe I had overlooked something. I... (4 Replies)
Discussion started by: l_p
4 Replies
Login or Register to Ask a Question