Help with this easy problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with this easy problem
# 1  
Old 10-20-2010
Help with this easy problem

hi people. i need assist in this quite easy problem. i have a text as:
Code:
cell112-1/2/3/4/5/6 4
cell156-1/2/3/4 7
cell197-1/2/3 6
cell215-1/2/3/4/5/6 9
cell235-1/2/3 5
cell354-1/2/3/4/5 8
cell355-1 4
...
cell446-1/2/3/4/5/6 5

the script should check second coloumn in each line and

if it is 4; it must write 1st option appending to "cellXXX"
if it is 5; it must write 2st option appending to "cellXXX"
if it is 6; it must write 3st option appending to "cellXXX"
if it is 7; it must write 4st option appending to "cellXXX"
if it is 8; it must write 5st option appending to "cellXXX"
if it is 9; it must write 6st option appending to "cellXXX"

so the result in above example should be:
Code:
cell1121
cell1564
cell1973
cell2156
cell2352
cell3545
cell3551
...
cell4462

# 2  
Old 10-20-2010
Try:
Code:
awk -F'[-/ ]' '$0=$1$($NF-2)' infile

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-20-2010
my machine is solaris and i have to use nawk instead of awk. but; this is the error:
Code:
nawk -F'[-/ ]' '$0=$1$($NF-2)' /home/gc_sw/down.txt

nawk: trying to access field -2
input record number 1, file /home/gc_sw/down.txt
source line number 1

# 4  
Old 10-20-2010
I suspect your actual input file does not conform to specification. Can you post a bit of your actual input file?
# 5  
Old 10-20-2010
my actual input file is completely as:

Code:
cell112-1/2/3/4/5/6 4
cell156-1/2/3/4 7
cell197-1/2/3 6
cell215-1/2/3/4/5/6 9
cell235-1/2/3 5
cell354-1/2/3/4/5 8
cell355-1 4
...
cell446-1/2/3/4/5/6 5

# 6  
Old 10-20-2010
works fine for me on Solaris with snippet you gave.

What is the case though, is that if the last field is less than 2 then $NF-2 will be negative....

so your data is the issue here.. have you additional requirements in the case the last field has values less than 4 ?
# 7  
Old 10-20-2010
I get:
Code:
cell1121
cell1564
cell1973
cell2156
cell2352
cell3545
cell3551
cell4462

But I had to take out the line with the dots...
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

[HELP] Easy task

I have a simple task for my school work. I'm new with unix, so i need help. I need to write a scenario. Task is. From created txt file read first 3 words and create a 3 catalogs with those 3 words. 2 of those new catalogs should be transferred to other directory. If someone could help me just... (1 Reply)
Discussion started by: justynykas
1 Replies

2. Shell Programming and Scripting

Easy edit problem: interchange columns

hi; my file1.txt: cell137 1 cell337 1 cell355 1 cell355 3 cell360 1 cell360 2 cell360 3 my file2.txt: ... cell137 1 20.64.1.97 cell137 2 20.64.1.97 cell137 3 20.64.1.97 ... cell337 1 20.64.1.113 cell337 2 20.64.1.113 cell337 3 20.64.1.113 (4 Replies)
Discussion started by: gc_sw
4 Replies

3. UNIX for Dummies Questions & Answers

Need help on installing an EASY to use and easy to install command line text editor

Hi again. Sorry if it seems like I'm spamming the boards a bit, but I figured I might as well ask all the questions I need answers to at once, and hopefully at least get some. I have installed Solaris 10 on a server. The default text editors are there (vi, ex, ed, maybe others, I know emacs is... (4 Replies)
Discussion started by: EugeneG
4 Replies

4. UNIX for Dummies Questions & Answers

Help with an 'easy' script

Hello guys, Forgive me if I duplicate the post but i think i make a mistake choosing the correct forum. I need some help to solve a little problem, I have a big file with almost 100.000 lines of data, here is an example of line: 100099C01101C00000000059399489283CREMOVISTAR_TX ... (3 Replies)
Discussion started by: lestat_ecuador
3 Replies

5. Shell Programming and Scripting

easy script

a script, cheer that prints its parameter as shown in the example below. eg: $ cheer U N I X Give me a U! U! Give me a N! N! Give me a I! I! Give me a X! X! #!/bin/sh for letter do echo "Give me a $letter!";echo "$letter!" done this is the code i used for the above script (2 Replies)
Discussion started by: problems
2 Replies

6. UNIX for Dummies Questions & Answers

Easy one

Hi This is going to sound so easy to some of you out there... trying to copy and paste lines to an existing script - is there a command that i can use that overwrites the existing text there? Cheers (11 Replies)
Discussion started by: vcardo10
11 Replies

7. UNIX for Dummies Questions & Answers

bad substitution problem (easy question)

hi, what i want to do is to convert all the txt file under my directory to the properties file using the native2ascii command. however, when i run my script, i got bad substitution error. what's wrong with my script ? pls help. thanks #!/bin/sh curDIR=`pwd` oldExt='txt'... (10 Replies)
Discussion started by: champion
10 Replies
Login or Register to Ask a Question