perl oneliner to cut the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl oneliner to cut the file
# 1  
Old 01-25-2012
perl oneliner to cut the file

Hi
I have a file say text.txt and has data as below.

text.txt
-------
Code:
/abc/def/tom/hanks
/abc/def/al/pacino
/def/dgg/matt/damon

Now I have to cut the field 3 and field 4 treating / as delimiter and save in the same file. Below is the how the output should be in the same file.
I would appreciate if some one could provide me a perl onliner

text.txt
-------
Code:
tom/hanks
al/pacino
matt/damon

Thanks very much
Matt

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 01-25-2012 at 12:26 PM..
# 2  
Old 01-25-2012
Code:
$
$
$ cat test.txt
/abc/def/tom/hanks
/abc/def/al/pacino
/def/dgg/matt/damon
$
$
$ perl -lne 'print join "/", (split/\//)[3,4]' test.txt
tom/hanks
al/pacino
matt/damon
$
$ # or using regex
$
$ perl -plne 's|^(/\w+){2}/||' test.txt
tom/hanks
al/pacino
matt/damon
$
$ # For inline editing
$
$ perl -i.bak -pe 's|^(/\w+){2}/||' test.txt
$
$ cat test.txt
tom/hanks
al/pacino
matt/damon
$
$ # Original file is backed up as "test.txt.bak"
$ cat test.txt.bak
/abc/def/tom/hanks
/abc/def/al/pacino
/def/dgg/matt/damon
$
$

tyler_durden

Last edited by durden_tyler; 01-25-2012 at 01:38 PM..
This User Gave Thanks to durden_tyler For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cut first value after underscore and replace first two digit with zero in perl

Like I have below string XX_49154534_491553_201_122023_D XX_49159042_491738_201_103901_D and the expected output would be 0154534 0159042 XX and 49 can be dynamic. (1 Reply)
Discussion started by: nadeemrafikhan
1 Replies

2. Shell Programming and Scripting

awk - oneliner vs multiple line .

Dear Awk Experts, I can write oneliner some time but find difficulty writing like a flow of a program in multiple lines, could you please help how to write awk program , like long awk programs, any help to start with, For example, How to write this in multiple line: ls -l | awk... (6 Replies)
Discussion started by: rveri
6 Replies

3. Shell Programming and Scripting

Oneliner ---split string to character by piping shell output to perl

Hello, I was trying to split a string to characters by perl oneliner. echo "The quick brown fox jumps over the lazy dog" | perl -e 'split // ' But did not work as with bash script pipe: echo "The quick brown fox jumps over the lazy dog" | fold -w1 | sort | uniq -ic 8 1 T 1... (6 Replies)
Discussion started by: yifangt
6 Replies

4. Shell Programming and Scripting

nawk oneliner to find and replace a pattern

Hi, I need to replace the ip 1.1.1.1 with the name test.sol.box . I have tried and come up with following code. do we have any other way of doing this with nawk?? Data: #This is a test setup.Please enter your values and corresponding port number here ########################## Server Host... (5 Replies)
Discussion started by: chidori
5 Replies

5. UNIX for Dummies Questions & Answers

oneliner for adding header and trailer

for example, i have a file with below content: 123413 866688 816866 818818 i want the output as: This is header 123413 866688 816866 818818 This is trailer i am able to achieve it using a bash script. (2 Replies)
Discussion started by: pandeesh
2 Replies

6. Shell Programming and Scripting

need a df -h oneliner..

can any one suggest some one liner such that when executed will display df -h output of the mount points that are 90% or more that it.. and not the remaining.. (7 Replies)
Discussion started by: chidori
7 Replies

7. Shell Programming and Scripting

perl oneliner not works .pl script

I am trying to take first 3 columns in a file which matches the word "abc", but i am getting the below error, <error> Global symbol "@F" requires explicit package name at ./new.pl </error> whereas when i give the below,grep abc /home/test/file.txt|perl -lane 'print \"$F $F $F\" in unix prompt... (4 Replies)
Discussion started by: anspks
4 Replies

8. Shell Programming and Scripting

Cut Data In Bigfile with Perl

I want to create new file for START-END but i know NO 0003/02 only one in file Ex. Data FILE Data.txt (Data ~1,000,000 Line) I use Script perl perl -lne '$/="END";print $_."END" if /0003\/02/' fileOut put script perl but I want create Out put All No in START-END have NO 0003/02 Please... (8 Replies)
Discussion started by: kittiwas
8 Replies

9. Shell Programming and Scripting

perl regex multi line cut

hello mighty all there's a file with lots of comments.. some of them looks like: =comment blabla blablabla bla =cut i'm trying to cut this out completely with this code: $line=~s/^=.+?=cut//sg; but no luck also tryed to change it abit but still I don't understand how the... (9 Replies)
Discussion started by: tip78
9 Replies

10. Shell Programming and Scripting

oneliner:sing SED on a specific column

is this possible a one liner sed command. I have a text file ex. from : xxx yyy ZZZ /test/devl/aasdasd.log1 xxx yyy ZZZ /test/devl/aasdasd.log2 to : xxx yyy ZZZ /test/prod/aasdasd.log1 xxx yyy ZZZ /test/prod/aasdasd.log2 and I want to sed only the fourth column sed 's/devl/prod/g' ... (8 Replies)
Discussion started by: chaseeem
8 Replies
Login or Register to Ask a Question