Cut command issue with shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut command issue with shell script
# 1  
Old 11-05-2013
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.

Code:
 
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 don't need,
 
awk '{print $0"|"}' filename
 
Input File:
492|3076394|SP40|1030028694570|20120125|20120207|13530392|||31||CHAPMAN PLACE|||COLCHESTER|ESSEX|CO4 5ZJ|ADI_001|7|_A|E|A|811|2|0151|EMEB|N|EMEB|N|EMEB|N|1|2|3|4|5|6|7|
 
 
Actual output:
492|3076394|SP40|1030028694570|20120125|20120207|13530392|||31||CHAPMAN PLACE|||COLCHESTER|ESSEX|CO4 5ZJ|7|_A|E|A|811|2|0151|EMEB|N|EMEB|N|EMEB|N
 
 
Expected output file:
 
I need to retain the pipe at last
492|3076394|SP40|1030028694570|20120125|20120207|13530392|||31||CHAPMAN PLACE|||COLCHESTER|ESSEX|CO4 5ZJ|7|_A|E|A|811|2|0151|EMEB|N|EMEB|N|EMEB|N|

I need to know if there is any other way to use the cut command to retain the pipe at end of the line

Awaiting for the suggestions on this,
# 2  
Old 11-05-2013
Try one of these
Code:
awk 'NR>1 {print $0"|"}'
awk '/|/ {print $0"|"}'

or these sed equivalents
Code:
sed '1!s/$/|/'
sed '/|/ s/$/|/'

# 3  
Old 11-05-2013
Hello,

Could you please try the following. Let us say check_fil1 is the file with the Input.

Code:
 sed 's/.EMEB|N|.*//g' check_fil1 | awk -vs1="|EMEB|N" '{print $0""s1""s1""s1"|"}'

Output will be as follows.

Code:
492|3076394|SP40|1030028694570|20120125|20120207|13530392|||31||CHAPMAN PLACE|||COLCHESTER|ESSEX|CO4 5ZJ|ADI_001|7|_A|E|A|811|2|0151|EMEB|N|EMEB|N|EMEB|N|


Thanks,
R. Singh
# 4  
Old 11-05-2013
Hi Ravinder,

Thanks very much for your response, but we just can't hard code any characters in the below command

Code:
 
 sed 's/.EMEB|N|.*//g' check_fil1 | awk -vs1="|EMEB|N" '{print $0""s1""s1""s1"|"}'

because the file would have n number of lines with different strings for example the one as shown below,

ZHV|2205|D0217001|T|TXU|Z|PAN|20120127002121||||OPER|
754|47406|
492|3076394|SP40|1030028694570|20120125|20120207|13530392|||31||CHAPMAN PLACE|||COLCHESTER|ESSEX|CO4 5ZJ|ADI_001|7|_A|E|A|811|2|0151|EMEB|N|EMEB|N|EMEB|N|1|2|3|4|5|6|7|
492|3076367|SP20|1030078044294|20120125|20120127|13530396|TESCO EXPRESS||117||BRICKHILL DRIVE|||BEDFORD|BEDFORDSHIRE|MK41 7QF|2001|86|_A|D|C|845|||UKDC|H|UKDC|H|EELC|H|1|2|3|4|5|6|7|
ZPT|2205|9||2|20120127002121|

So, if there is any cut command or awk command to add the | at end of the line by checking the number of feilds and if feilds exceeds 30+ then only the pipe should be added.

Or

While using the below command I need to have a pipe enabled at the end

Code:
 
cut -d '|' -f 1-17,19-31 filename

Thanks very much and awaiting for suggestions
# 5  
Old 11-05-2013
You may try this also for given input in #1 this will work

Code:
$ cat file
492|3076394|SP40|1030028694570|20120125|20120207|13530392|||31||CHAPMAN PLACE|||COLCHESTER|ESSEX|CO4 5ZJ|ADI_001|7|_A|E|A|811|2|0151|EMEB|N|EMEB|N|EMEB|N|1|2|3|4|5|6|7|

Code:
$ awk  'x<3{printf $0 OFS}$1=="N"{++x}x>2{printf "\n";exit}'  RS="|" OFS="|" file

Resulting
Code:
492|3076394|SP40|1030028694570|20120125|20120207|13530392|||31||CHAPMAN PLACE|||COLCHESTER|ESSEX|CO4 5ZJ|ADI_001|7|_A|E|A|811|2|0151|EMEB|N|EMEB|N|EMEB|N|

OR

You can try like this
1-17 and 19-31
Code:
$ awk -F"|" '{for(i=1;i<=NF;i++){if(i<=17 || i>=19 && i<=31)printf $i OFS}printf RS}' OFS="|" file

OR

Code:
$ cut -d '|' -f 1-17,19-31 file | awk ' $0=$0 "|" '


Last edited by Akshay Hegde; 11-05-2013 at 08:08 AM..
# 6  
Old 11-05-2013
Thanks very much Akshay, unfortunately after trying all those they don't work as expected,
Lets say input file test.txt contains as shown below

Code:
 
test.txt
754|47406|
492|3076394|SP40|1030028694570|20120125|20120207|13530392|||31||CHAPMAN PLACE|||COLCHESTER|ESSEX|CO4 5ZJ|ADI_001|7|_A|E|A|811|2|0151|EMEB|N|EMEB|N|EMEB|N|1|2|3|4|5|6|7|

Expected output
754|47406|
492|3076394|SP40|1030028694570|20120125|20120207|13530392|||31||CHAPMAN PLACE|||COLCHESTER|ESSEX|CO4 5ZJ|7|_A|E|A|811|2|0151|EMEB|N|EMEB|N|EMEB|N|
 
That is it should remove 18th feild and all the feilds from after 31th feild

Thanks very much and your help is much appreciated!!
# 7  
Old 11-05-2013
Did you try what I posted in #5 ?
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

FTP command issue in shell script

Hi All I am using following code in my shell script to send the file to a mainframe server. ftp -in $FTP_IP_SEND <<END_OF_FTP >$LOG_DIR/ProviderExportFTP.log quote user $FTP_USER_SEND quote pass $FTP_PASS_SEND ascii send ./ProviderExport.txt 'PROJ.PDRCACTS.FD87050.EXPORT' (REPLACE... (3 Replies)
Discussion started by: Renjith180282
3 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

korn shell to cut command output

hello, i use following command: md5sum TEST.xml the output looks like: 900hjidur84hjr938ikv TEST.xml as you can see, the first part is the md5 code, the second part is the file name, but i only want the first part(md5 code), and save it to a file, how to do that? thanks. (2 Replies)
Discussion started by: zbc
2 Replies

6. Shell Programming and Scripting

Cut in shell script

Hi All, Am a Beginner to shell scripting, so please pardon my question. Below is the file format of file a.txt a.txt F=3 a|b|c|d 1|2|3|4 as|as|asd|asd Aas|as|asd|ada In the above file format, i have record count of total no of records excluding the header file here 3 , this varies... (2 Replies)
Discussion started by: sp999
2 Replies

7. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: asutoshch
5 Replies

8. Shell Programming and Scripting

Problem using cut command in shell script

I'm new to shell programming, and am having a problem in a (Korn) shell program, which boils down to this: The program reads a record from an input file and then uses a series of "cut" commands to break the record into parts and assign the parts to variables. There are no delimiters in the... (2 Replies)
Discussion started by: joroca
2 Replies

9. Shell Programming and Scripting

How to cut, concatenate data in Shell Script

Hello All,, I'm very new to Unix and I'm more in SAP ABAP. I have a requirement for a shell script which will accept one parameter as Input file name(with directory) e.g : /sapdata/deubank/dbdirect/out/deu01deupe/in/deu01deupe20051207111320.pmt In the shell script I have to make two more... (2 Replies)
Discussion started by: vasan_srini
2 Replies

10. UNIX for Dummies Questions & Answers

Shell script: Cut / (slash) character in string

I have a string "\/scratch\/databases\". I want to have a new string "\/scratch\/databases" by cutting last '\' character using shell script. I can't do this Please help me. Thanks in advance ThuongTranVN (4 Replies)
Discussion started by: ThuongTranVN
4 Replies
Login or Register to Ask a Question