Multiple beginner's problems


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple beginner's problems
# 1  
Old 02-04-2015
Multiple beginner's problems

Hello!
I started how to write scripts (basic one) in bash. I would like to post my problems here (i will edit topic if i will have any news problems)

1] i have a txt file like:
Code:
32323   4.2
12121   1.4

...
first column is ,,id" and second is evaluation i would like to replace numbers :
A-0, B-1... and after that i would like to write changed coulumn 1 and column 2
Code:
kol=$(cut -f1 $1 | tr 0-9 A-J)

change my first column and save result in variable how can i display it now?

Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output as well as for sample code segments.

Last edited by Don Cragun; 02-04-2015 at 10:25 PM.. Reason: Add CODE tags.
# 2  
Old 02-04-2015
Is this what you are trying to do?

Code:
$ cat file.txt
32323 4.2
12121 1.4
$ cat file.txt | tr 0-9 A-J
DCDCD E.C
BCBCB B.E
$ cat file.txt | tr 0-9 A-J > file2.txt
$ cat file2.txt
DCDCD E.C
BCBCB B.E

# 3  
Old 02-04-2015
yes but u changed 2nd column too and its not good. I want to change only 1st column example display should be:
Code:
AABDA    2.5

Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output as well as for sample code.

Last edited by Don Cragun; 02-04-2015 at 10:26 PM.. Reason: Add CODE tags,
# 4  
Old 02-04-2015
Quote:
Originally Posted by Kokoos
yes but u changed 2nd column too and its not good. I want to change only 1st column example display should be:
Code:
AABDA    2.5

Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output as well as for sample code.
With the translation table you provided, how would you ever get the output shown above from the input you provided in the 1st post in this thread:
Code:
32323   4.2
12121   1.4

To get AABDA you would need to start with 00130, not 32323 or 12121.

I'm confused. What is really going on here?
# 5  
Old 02-05-2015
omg... it was just random example but if u want here you go...

Code:
MY FILE:
1112  2.5
2222  3.0
0555  9.0

OUTPUT:
BBBC  2.5
DDDD  3.0
AFFF  9.0

btw this i what i was looking for... :
Code:
#!/bin/bash
file=$1
shift

if [[ ! -f $file ]]; then
        echo "Plik $file nie istnieje."
        exit
fi

k=0
for x in `gawk "{print}" $file`; do
        if [[ $k == 0 ]]; then
                echo -n "`echo " $x" | tr [0-9] [A-J]` "
                k=1
        else
                echo " $x"
                k=0
        fi
done

Is there any way to make it 1 line command?
# 6  
Old 02-05-2015
If you want some decent error checking in place, you can't do it in one line. Nor if you want your code to be legible (for yourself as well as for others).
For just the translation, try
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

---------- Post updated at 08:36 ---------- Previous update was at 08:31 ----------

BTW, gawk "{print}" $file is an enormous effort for just catting that file, which in turn were useless as well as you can read the redirected file...
# 7  
Old 02-05-2015
Of course it could also be written using a single call to awk, but it isn't nearly as compact as a shell for loop calling tr:
Code:
awk '
BEGIN {	t["0"] = "A"; t["1"] = "B"; t["2"] = "C"; t["3"] = "D"; t["4"] = "E";
	t["5"] = "F"; t["6"] = "G"; t["7"] = "H"; t["8"] = "I"; t["9"] = "J";
}
{	x = " "
	for(i = 1; i <= length($1); i++)
		x = x (((f = substr($1, i, 1)) in t) ? t[f] : f)
	printf("%s  %s\n", x, $2)
}' file

which could also be written on a single line as:
Code:
awk 'BEGIN{t["0"]="A";t["1"]="B";t["2"]="C";t["3"]="D";t["4"]="E";t["5"]="F";t["6"]="G";t["7"]="H";t["8"]="I";t["9"]="J";}{x=" ";for(i=1;i<=length($1);i++)x=x (((f=substr($1,i,1)) in t)?t[f]:f);printf("%s  %s\n",x,$2)}' file

but I MUCH prefer readable to "fits" on a single line.
If you want to try either of these on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.

In the future, when you present more scripts, please show us samples of the data you have, the data you want, and the code you have tried as well as a description of the processing to be performed. Since extensions to the "standard" utilities vary from system to system and shell to shell, please also tell us what operating system and shell you're using.
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