Subsitute from a position till end of line.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Subsitute from a position till end of line.
# 1  
Old 05-16-2009
Subsitute from a position till end of line.

Hi,
Having a following file's content, lets say:

ABC|ANA|LDJ|||||DKD||||||
AJJ|KKDD||KKDK||||||||||||
KKD||KD|||LLLD||||LLD|||||

Problem:

Need to replace pipes from 8th occurrence of pipe till end.

so the result should be:
ABC|ANA|LDJ|||||DKD
AJJ|KKDD||KKDK||||
-------
-------

Can someone give me one line sed or may one line perl script?

Thanks in advance !
Smilie

Last edited by _Noprofi; 05-16-2009 at 02:41 AM..
# 2  
Old 05-16-2009
if you have Python
Code:
# more file
ABC|ANA|LDJ|||||DKD||||||
AJJ|KKDD||KKDK||||||||||||
KKD||KD|||LLLD||||LLD|||||

# python -c "for line in open('file').read().split('\n'):print '|'.join(line.split('|')[:9])"
ABC|ANA|LDJ|||||DKD|
AJJ|KKDD||KKDK|||||
KKD||KD|||LLLD|||

# 3  
Old 05-16-2009
Don't have python

Sorry, I don't have python installed !
# 4  
Old 05-16-2009
Code:
cut -d \| -f1-8 FILENAME

# 5  
Old 05-16-2009
is there a way to achieve this using sed or perl

Thanks !
I used cut to achieve this which is quite slow. Is it possible to achieve this using sed or any other utility?
# 6  
Old 05-16-2009

Nothing is likely to be faster than cut. It's what cut is designed to do; other utilities will have much more overhead.
# 7  
Old 05-16-2009
Thanks !
I was wondering, couldn't we just replace all the pipes(|) from 8th occurrence till end of the line with a new line. I can use this command with Perl as well...I mean I have a Unix shell script in which cut is used but it is quite slow therefore want to simulate the same in Perl. I don't either want to use split and join in Perl. It looks to be quite a bit of extra work.Therefore looking for a utility, may be one liner to get this done....

Any thought!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut line from file till the end

Hi I have a 57 line text file and I want to cut first 6 line assigned it to a veriable and again cut next 6 line assigned to same variable till the time file have left 0 line. Please let me know how I can do in scripting. Thanks Sanjay (6 Replies)
Discussion started by: Sanjay2121
6 Replies

2. Shell Programming and Scripting

Remove the spaces at the end of a line starting from a fixed position

I want to remove the trailing spaces at the end of each line starting from a particular position(using ksh script). For example, in the attached file, I want to remove all the spaces starting from the position 430 till the end. The space has to be removed only from the 430th position no matter in... (3 Replies)
Discussion started by: Suryaaravindh
3 Replies

3. Shell Programming and Scripting

Remove line starting from space till end.

Hi, I have a code tag, from which i have the below snippet: intelrpt.GetCMB_FB type=ODBC> intelrpt.GetCMB_FB type=SYBASE> I want the output like: intelrpt.GetCMB_FB intelrpt.GetCMB_FB That is remove the lines starting from WHITESPACE till end. Please help. I am new to... (7 Replies)
Discussion started by: anupdas
7 Replies

4. Shell Programming and Scripting

Grep from a starting line till the end of the file

Hi Folks, I got to know from this forums on how to grep from a particular line say line 6 awk 'NR==6 {print;exit}' But how do i grep from line 6 till the end of the file or command output. Thanks, (3 Replies)
Discussion started by: Mr. Zer0
3 Replies

5. Shell Programming and Scripting

take all characters from end to start till a fullstop

e.g. "hello.all" to be transformed to "all" :b: (6 Replies)
Discussion started by: hakermania
6 Replies

6. UNIX for Dummies Questions & Answers

find if a position is between a given start and end position

Hi, I am a newbie in unix programming so maybe this is a simple question. I would like to know how can I make a script that outputs only the values that are not between any given start and end positions Example file1: 2 30 40 80 82 100 file2: ID1 1 ID2 35 ID3 80 ID4 81 ID6... (9 Replies)
Discussion started by: fadista
9 Replies

7. UNIX for Advanced & Expert Users

Urgent Help required : awk/sed help to find pattern and delete till end of line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (1 Reply)
Discussion started by: rajan_san
1 Replies

8. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (2 Replies)
Discussion started by: rajan_san
2 Replies

9. Shell Programming and Scripting

check position of end of line(URGENT PLS)

I Have to check in a file that all the lines are ending at same posiotin. Ex : line 1 is ending at position 88 line 2 should at same position i.e 88 Thanks in advance (6 Replies)
Discussion started by: evvander
6 Replies

10. Shell Programming and Scripting

check position of end of line for some specific lines

-------------------------------------------------------------------------------- Have to check in a file that the lines starting with 620 and 705 are ending at same posiotin. 82012345 62023232323 70523949558 62023255454 9999 In the above lines, i have to check the lines starting... (1 Reply)
Discussion started by: senthil_is
1 Replies
Login or Register to Ask a Question