Cut command issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut command issue
# 1  
Old 11-06-2013
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,

Code:
 

Input:
 
354|||||CORPORTATION||||NENE PARADE|||WISBECH|CAMBRIDGESHIRE|PE13 3BY|100001|
 
I am removing the 16th feild by using the cut command as shown,
 
cut -d '|' -f 1-15 test.txt
 
354|||||CORPORTATION QUAY||||NENE PARADE|||WISBECH|CAMBRIDGESHIRE|PE13 3BY
 
But I wanna append the extra pipe right after the cut command to end the line with |PE13 3BY| rather |PE13 3BY
 
Expected output:
 
354|||||CORPORTATION QUAY||||NENE PARADE|||WISBECH|CAMBRIDGESHIRE|PE13 3BY|

Thanks very much,
# 2  
Old 11-06-2013
What have you tried? Can you not adapt one of the solutions from your previous thread on a similar subject?
# 3  
Old 11-06-2013
Once again dumping up...
why don't you adapt solution given in your old threads ?
try

Code:
$ cut -d '|' -f 1-15 test.txt | awk ' $0=$0"|" '

# 4  
Old 11-06-2013
Thanks very much Akshay,

I tried the above one Akshay, but if we have more than one line then its appending the pipe to all the lines rather than appending a pipe if feilds are more than 15,

Code:
 
$ [XXXX] $ cut -d '|' -f 1-15 EPN_D171_1 | awk ' $0=$0"|" '
 
748|4748||
352|4122054|SP29|1012869851946|20130925||
354|||||CORPORTATION QUAY||||NENE PARADE|||WISBECH|CAMBRIDGESHIRE|PE13 3BY|

# 5  
Old 11-06-2013
Try like this...change NF according to your requirement

NF --> The number of fields in the current input record.

Code:
$ cut -d '|' -f 1-15  file | awk 'NF>15{$0=$0"|"}1'

# 6  
Old 11-06-2013
Try:
Code:
sed 's/[^|]*|//16' file

or for example
Code:
awk 'sub(/[^|]*\|$/,x)' FS=\| OFS=\| file


--
Some awks can do this:
Code:
awk '{$(--NF)=x}1' FS=\| OFS=\| file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 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. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 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. UNIX for Dummies Questions & Answers

Cut pid from ps using cut command

hay i am trying to get JUST the PID from the ps command. my command line is: ps -ef | grep "mintty" | cut -d' ' -f2 but i get an empty line. i assume that the delimiter is not just one space character, but can't figure out what should i do in order to do that. i know i can use awk or cut... (8 Replies)
Discussion started by: ran ber
8 Replies

5. Shell Programming and Scripting

Issue with cut and paste

let i have A file and B file A has contains 4 fields as below ---------------- f1 f2 f3 f4 B file consists of 5 fields as below -------------------- f5 f6 f7 f8 f9 need to display as below output: f5 f1 f3 f8 f9 (2 Replies)
Discussion started by: ANSHUMAN1983
2 Replies

6. 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

7. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

8. 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

9. Shell Programming and Scripting

Simple Cut issue

I have a long string that looks something like this.... <string>http://abc.com/40/20/zzz061-3472/dP3rXLpPPV2KC846nJ4VXpH7jt4b3LJgkL/tarfile_date.tar</string> I need to but the tar file name. So I need to put between "/" and ".tar</string>". My desired result should be "tarfile_date". (7 Replies)
Discussion started by: elbombillo
7 Replies
Login or Register to Ask a Question