Deleting text start at 2nd line....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting text start at 2nd line....
# 1  
Old 12-20-2012
Deleting text start at 2nd line....

How can I delete the first '2' characters in the 2nd line and every fourth line thereafter (Means line no.s 2, 6, 10...so on).
For example if the following is my Input
Code:
treieieiei
trrpepepepep
tyeueueue
tyeueueeu
tyeyeyeye
tryryry
tyeyey
tyrtyty
tytyty
tututu
tututututu

Output should be:
Code:
treieieiei
..rpepepepep
tyeueueue
tyeueueeu
tyeyeyeye
..yryry
tyeyey
tyrtyty
tytyty
..tutu
tututututu

PS: I have put '..' only to illustrate.
# 2  
Old 12-20-2012
Code:
awk '(NR-2)%4 == 0{$0=substr($0,3)}1' file


Last edited by pamu; 12-20-2012 at 06:15 AM.. Reason: added code...
This User Gave Thanks to pamu For This Post:
# 3  
Old 12-20-2012
Code:
awk -v a=2 'NR==a{sub("^..","");a+=4}1' input.txt

This User Gave Thanks to itkamaraj For This Post:
# 4  
Old 12-20-2012
@pamu: Guess you have got the requirements wrong! Moreover, you don't need to check for (NR-2)%4 and NR==2. The former would do the task! Smilie
# 5  
Old 12-20-2012
Quote:
Originally Posted by PikK45
@pamu: Guess you have got the requirements wrong! Moreover, you don't need to check for (NR-2)%4 and NR==2. The former would do the task! Smilie
No. I got the requirements right..

Please check.

Quote:
Originally Posted by cs_novice
How can I delete the first '2' characters in the 2nd line and every fourth line thereafter (Means line no.s 2, 6, 10...so on).
For example if the following is my Input
PS: I have put '..' only to illustrate.
And for method. Come on man. This is one way of doing and he has another way of doing.
It's up to you which method you want to choose.Smilie
# 6  
Old 12-20-2012
Cool down Smilie Smilie

I got it wrong. I just looked at the IP & OP and came to a wrong conclusion. You rock with "awk" Smilie Smilie

however, the NR==2 is not required in your method :P
# 7  
Old 12-20-2012
Quote:
Originally Posted by PikK45
however, the NR==2 is not required in your method :P
Just tried with the your suggestion. modified in my previous code.

ThanksSmilie

pamu

Last edited by pamu; 12-20-2012 at 06:14 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting all the N lines after the line in which text is found.

Hi! I want to delete N (say 10) lines after the line which text is found in a file "A".Also to delete the line in which the text is found. Only one occurrence of the search string in the file "A" The text to be deleted is in another text file "B". All the search texts in the file "B" are in... (3 Replies)
Discussion started by: shahid1632
3 Replies

2. Shell Programming and Scripting

Add text at start and ending of every line

Hi all, Is there other way to Add text at start and ending of every line? here my script: cat file.txt |awk '{print "<p align=\"justify\">"$0"</p>"}' but the problem they put including white spaces, I only need those line have a sentence or text not an skip all have empty string or have... (7 Replies)
Discussion started by: lxdorney
7 Replies

3. UNIX for Dummies Questions & Answers

Deleting a pattern in UNIX without deleting the entire line

Hi I have a file: r58778.3|SOURCES={KEY=f665931a...,fw,221-705}|ERRORS={16_1:T,30_1:T,56_1:C,57_1:T,59_1:A,101_1:A,115:-,158_1:C,186_1:A,204:-,271_1:T,305:-,350_1:C,368_1:G,442_1:C,472_1:G,477_1:A}|SOURCE_1="Contig_1092402550638"(f665931a359e36cea0976db191ff60ff09cc816e) I want to retain... (15 Replies)
Discussion started by: Alyaa
15 Replies

4. Shell Programming and Scripting

deleting the part of the file(overwrite) using start and end point

here is the contents of bigfile.sql CREATE TABLE `Table11` ( `id` int(11) NOT NULL , `entityName` enum('Lines','EndUsers') COLLATE utf8_unicode_ci NOT NULL, `parentAllianceMigrationProjectId` varchar(255) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=2000 DEFAULT CHARSET=utf8... (30 Replies)
Discussion started by: vivek d r
30 Replies

5. Shell Programming and Scripting

help with finding text and deleting line

HI All, I need to search for a particular pattern input by the user in order to delete the line. My username.txt has username@email.com:John:149.0.3.4:1 username1@email.com:Harry:149.0.3.4:1 username1@email.net:Alex:149.0.3.4:1 username1@email.edu:Nemo:149.0.3.4:1 The program i written ... (3 Replies)
Discussion started by: ichar
3 Replies

6. Shell Programming and Scripting

How to input text on every 2nd line?

I have a file in two columns consisting of only numbers. I would like to put a string (the same textstring) on every second line, so that 501006 420.000 401602 165.000 . . . becomes: *Some_text 501006 420.000 *Some_text 401602 165.000 *Some_text . . . Are you up for it? (7 Replies)
Discussion started by: Medova
7 Replies

7. UNIX for Advanced & Expert Users

Sed - add text to start of line if 1st char anything but space

Problem: I have a lot of files, the files first line should always have 4 spaces before any text. Occasionally some of the files will miss the leading spaces and it's a problem. This is only in the first line. So if there are 4 spaces then text, do nothing. If there are not 4 spaces, add 4... (2 Replies)
Discussion started by: Vryali
2 Replies

8. Shell Programming and Scripting

print line if 2nd field exists in text

2 files, first one has 3 fields seperated by ||| and 2nd one is plain text. I want to copy the lines from the first file if the 2nd field is present anywhere in the text file. This is what I've tried, but I'm new to awk and shell scripting in general so it's kinda broken. #!/bin/awk -f BEGIN... (15 Replies)
Discussion started by: FrancoisCN
15 Replies

9. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

10. Shell Programming and Scripting

How to print the lines after 2nd line from text file

Hi All, I need to print the lines after 2nd line irrespective of the number of lines in file if the file contents are like below ---------- root:!:0:0::/root:/usr/bin/ksh daemon:!:1:1::/etc: bin:!:2:2::/bin: sys:!:3:3::/usr/sys: adm:!:4:4::/var/adm: uucp:!:5:5::/usr/lib/uucp:... (6 Replies)
Discussion started by: ram1729
6 Replies
Login or Register to Ask a Question