Replacing a character with a number based on lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing a character with a number based on lines
# 1  
Old 03-11-2013
Replacing a character with a number based on lines

Hi,

I am in need of help for the two things which is to be done.

First, I have a file that has around four columns. The first column is filled with letter "A".

There are around 400 lines in the files as shown below.

Code:
A  1   5.2   3.2
A  2   0.2   4.5
A  1   2.2   2.2
A  5   2.1   3.2
....................

I want to replace the letter " A " with the number "2" but only for the lines from 201st line to 300th line. So, as an ouput , I should have letter " A " in the first column for the first 200 lines , then the number " 2 " should be displayed for the first column from the line 201st to 300th line instead of letter "A". From 301st line to 400th line, the first column should be displayed as " A"


Second thing, is that, I have a file with four columns. It consists of around 400 lines.

The first column consists of letter "A" or " B ". For example:


Code:
A  1   5.2   3.2
A  2   0.2   4.5
B  0   5.5   11
A  1   2.2   2.2
B  1   9.9   0.01
A  5   2.1   3.2

I want the letter " A" in the first column to be replaced with number " 1" and the letter " B" in the second column to be replace with number " 2" . The expected result should be:

Code:
1  1   5.2   3.2
1  2   0.2   4.5
2  0   5.5   11
1  1   2.2   2.2
2  1   9.9   0.01
1  5   2.1   3.2

# 2  
Old 03-11-2013
Code:
sed '
  201,300s/^A/1/
 ' in_file >out_file
 
sed '
  s/^A/1/
  t
  s/^B/2/
 ' in_file >out_file

The t skips the B test if it is an A, for speed.
# 3  
Old 03-11-2013
Just re-formatting as one-liners (basically same content as previous correct response):
Code:
sed '201,300 s/^A/1/' in_file_1 > out_file_1

Code:
sed 's/^A/1/; s/^B/2/' in_file_2 > out_file_2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Select lines based on character length

Hi, I've got a file like this: 22 22:35645163:T:<CN0>:0 0 35645163 T <CN0> 22 rs140738445:20902439:TTTTTTTG:T 0 20902439 T TTTTTTTG 22 rs149602065:40537763:TTTTTTG:T 0 40537763 T TTTTTTG 22 rs71670155:50538408:TTTTTTG:T 0 50538408 T TTTTTTG... (3 Replies)
Discussion started by: zajtat
3 Replies

2. Shell Programming and Scripting

Print lines based on line number and specified condition

Hi, I have a file like below. 1,2,3,4,5,6,7,8,9I would like to print or copied to a file based of line count in perl If I gave a condition 1 to 3 then it should iterate over above file and print 1 to 3 and then again 1 to 3 etc. output should be 1,2,3 4,5,6 7,8,9 (10 Replies)
Discussion started by: Anjan1
10 Replies

3. UNIX for Dummies Questions & Answers

Split file based on number of blank lines

Hello All , I have a file which needs to split based on the blank lines Name ABC Address London Age 32 (4 blank new line) Name DEF Address London Age 30 (4 blank new line) Name DEF Address London (8 Replies)
Discussion started by: Pratik4891
8 Replies

4. Shell Programming and Scripting

Joining lines in TXT file based on first character

Hi, I have a pipe delimeted text file where lines have been split over 2 lines and I need to join them back together. For example the file I have is similar to the following: aaa|bbb |ccc ddd|eee fff|ggg |hhh I ideally need to have it looking like the following aaa|bbb|ccc ddd|eee... (5 Replies)
Discussion started by: fuji_s
5 Replies

5. UNIX for Dummies Questions & Answers

Change a character based on its position number

Hi I have a text file that I want to change some of the characters based on their position. My file contain multiple lines and characters should be counted continuously line by line. For example, I want to convert the 150th T to C. What can I do? Here is a portion of my file:... (10 Replies)
Discussion started by: a_bahreini
10 Replies

6. Shell Programming and Scripting

Concatinating the lines based on number of delimiters

Hi, I have a problem to concatenate the lines based on number of delimiters (if the delimiter count is 9 then concatenate all the fields & remove the new line char bw delimiters and then write the following data into second line) in a file. my input file content is Title| ID| Owner|... (4 Replies)
Discussion started by: bi.infa
4 Replies

7. UNIX for Dummies Questions & Answers

Command to split the files based on the number of lines in it

Hello Friends, Can anyone help me for the below requirement. I am having a file called Input.txt. My requirement is first check the count that is wc -l input.txt If the result of the wc -l Input.txt is less than 10 then don't split the Input.txt file. Where as if Input.txt >= 10 the split... (12 Replies)
Discussion started by: malaya kumar
12 Replies

8. Shell Programming and Scripting

Delete lines based on line number

I have a file with ~200K lines, I need to delete 4K lines in it. There is no range. I do have the line numbers of the lines which I want to be deleted. I did tried using > cat del.lines sed '510d;12d;219d;......;3999d' file > source del.lines Word too long. I even tried... (2 Replies)
Discussion started by: novice_man
2 Replies

9. Shell Programming and Scripting

Replacing Character in a file based on element

Hi, I have file like below. Unix:/pclls/turc>cat tibc.property executeReceiver=Y executeSender=Y I want to replace executeSender=N in the file. My file should be like below. executeReceiver=Y executeSender=N I tried with the below command, its giving error. cat tibc.property |... (2 Replies)
Discussion started by: senthil_is
2 Replies

10. Shell Programming and Scripting

Merging lines based on occurances of a particular character in a file

Hi, Is there any way to merge two lines based on specific occurance of a character in a file. I am having a flat file which contains multiple records. Each row in the file should contain specified number of delimiter. For a particular row , if the delimiter count is not matched with... (2 Replies)
Discussion started by: mohan_tuty
2 Replies
Login or Register to Ask a Question