Multiple beginner's problems


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple beginner's problems
# 8  
Old 02-05-2015
Using Sed

Code:
# cat file
1234 2.3
1111 4.5
2341 6.7
# sed "h;s/ .*//;y/0123456789/abcdefghij/;G;s/\n.* / /" file
abcd 2.3
aaaa 4.5
bcda 6.7


Last edited by anbu23; 02-05-2015 at 04:35 AM..
# 9  
Old 02-05-2015
Do you insist on the translation table?
Code:
awk    '       {x = ""
                 for(i = 1; i <= length($1); i++)
                 x = x sprintf ("%c", 65+ substr($1, i, 1))
                 printf("%s  %s\n", x, $2)
}' file
BBBC  2.5
CCCC  3.0
AFFF  9.0

# 10  
Old 02-05-2015
I used a translation table because the input:
Code:
0123456789abcdABCD 	 10.9

should produce the output:
Code:
ABCDEFGHIJabcdABCD  10.9

instead of the output:
Code:
ABCDEFGHIJAAAAAAAA  10.9

If we knew that the first field on each line only contains digits (no punctuation and no alphabetic characters), I would do what you did. But that wasn't specified in the problem statement and the tr command used by the submitter more closely matches what I did with the translation table.

Of course, we're both doing the wrong thing (compared to the submitter's script) if some lines have more or less than 2 fields per line. The specification provided leaves a lot unsaid. So, we all make different assumptions about what the real requirements are...
# 11  
Old 02-06-2015
I really like this method (very simple):
Code:
while read A B REST; do echo -n $A | tr '0-9' 'A-J'; echo " " $B; done < file
BBBC  2.5
CCCC  3.0
AFFF  9.0

but if my file would like like:
Code:
2121   2.0  3232   32323   1212  2122  ...(n columns)
1111   4.2  1321   12341   2123  1245  ...
  .        .      .         .          .       .
  .        .      .         .          .       .

and i would like to ,,change" column nr X from range (0-n). Example (for column nr 5) output:
Code:
2121   2.0  3232   32323   1212  CBCC  ...(n columns)
1111   4.2  1321   12341   2123  BCEF  ...
  .        .      .         .          .       .
  .        .      .         .          .       .

# 12  
Old 02-06-2015
The awk script I suggested will work with whatever field you want. Just change both occurrences of $1 in that script (which specified field #1) to $(fn) where fn is an arithmetic expression that evaluates to the number of the field you want to modify (such as $5 for the fifth field or $(NF - 1) for the next to the last field) on each line.

Note, however, that $(NF - 1) won't work if you have some lines with less than 2 fields. If you have cases like that you would have to make the change conditional on having enough fields.

PS Note that in post #11, you said you wanted to work on column nr 5, but the output you showed modified column #6; not column #5. In awk, field numbers start at 1.

Last edited by Don Cragun; 02-06-2015 at 04:01 AM.. Reason: Add PS.
# 13  
Old 02-06-2015
Why don't you adapt the solution you prefer? Try
Code:
while read A B C D E F REST; do echo -n "$A  $B  $C  $D  $E  ";  echo -n $F | tr '0-9' 'A-J'; echo " " $REST; done < file
2121  2.0  3232  32323  1212  CBCC  ...(n columns)
1111  4.2  1321  12341  2123  BCEF  ...

Might become unwieldy for fields further down the line.
# 14  
Old 02-06-2015
i know i could make it like this A B C D E ... but if my file will have 1000 columns and i will want to change column nr 500 ? If i could use ,,for loop" to avoid writing A B C D ... it would be great

PS: i guess i will have another question in the evening but first i wanna try it by myself.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Help me please i am beginner

i have windows 8 host on Dell Laptop vmware 9 redhat 7.2 iso downloaded through redhat official site after installation on vm it only boots into text dont show graphics Please guide:( (1 Reply)
Discussion started by: hananabbas
1 Replies

2. Shell Programming and Scripting

Problems setting or exporting variable when using multiple commands from same line.

I am experimenting with some scripting as a way to learn more about it. I have a simple script that calls two other scripts. Each script echos some stuff to prove it ran and then sets a simple variable and exports it. I cannot get one of the variables to display back in the main calling script... (2 Replies)
Discussion started by: scottrif
2 Replies

3. Shell Programming and Scripting

Problems with Multiple Pattern String Matching

I am facing a problem and I would be grateful if you can help me :wall: I have a list of words like And I have a datafile like the box of the box of tissues out of of tissues out of the book, the the book, the pen and the the pen and the I want to find Patterns of “x.*x” where... (2 Replies)
Discussion started by: A-V
2 Replies

4. UNIX for Advanced & Expert Users

Surfing the Internet problems with multiple browsers

Could anyone explain why I am having trouble surfing the internet with both firefox and konqueror? Chromium seems to be the only browser that will work. I tried to create a new profile with firefox and that didn't work either. I can ping things just fine, I can download stuff with wget, I can ssh... (6 Replies)
Discussion started by: cokedude
6 Replies

5. UNIX for Dummies Questions & Answers

Problems Grepping within multiple Quotes

Hello, I have a block of code (XML) that I would like to grep for certain information. The basic format of the XML is the following repeated a few hundred times, each time with a unique ID: <Identifier ID="A" NAME="John Doe" AGE="32 Years" FAMILY="4" SEX="MALE"></Identfier> I would like to... (6 Replies)
Discussion started by: jl487
6 Replies

6. Shell Programming and Scripting

Beginner Help

I need to write a script to test a nsort c program. I have written 8 .txt files with different cases. Also 8 .txt files with expected outcome. The shell I have written always "test pass" for the first case but always "fail" for the rest... Here is a portion of my code (as I still don't know how to... (5 Replies)
Discussion started by: thibodeau
5 Replies

7. Shell Programming and Scripting

Beginner bash scripting - a few problems

Hey Guys, I am creating a bash script on my freeBSD box, the script should basically ask the user to enter a username and domain. The script will take this information and basically append alot of information to config files so the user can receive email from that domain and create a web site at... (1 Reply)
Discussion started by: traxy
1 Replies

8. UNIX for Dummies Questions & Answers

Beginner Help

hi guys, i have a DEl xps laptop cor 2 duo 2.2 i have vista installed on it i want to install a dual Boot UNIX on it.. can some one guide me ...cause i m tottaly new to UNIX i want to install unix on that laptop along with Vista.... thx any help would be deeply appreciated (sorry if i... (5 Replies)
Discussion started by: Farhan082
5 Replies

9. Solaris

Solaris x86 - multiple problems

Recently installed a custom PC with Solaris 10 u4, kernelpatch 127112-10. I was going to use this box as a NAT router using ipf. ipf however caused the box to panic multiple times. This in turn lead to a couple other issues that I would like some input of. - None of the panic's i've... (0 Replies)
Discussion started by: ba72
0 Replies

10. Programming

Beginner C

Anyone know where I can get started in C++ programming in unix? Any good free tutorials or websites to start at? I am okay in unix scripting but have never done c programming of any sort... What are the main advantages of using C++ ? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question