Need help with paste command using variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with paste command using variables
# 1  
Old 10-01-2013
Need help with paste command using variables

How can I accomplish this? I basically want to merge two variables onto the same line. I can do it with two FILES this way:

Code:
[ngreenfield@xxxxxx tmp]$ cat /tmp/users_in.list | awk -F "," '{print $2}' | cut -c -1 > first.initial
[ngreenfield@xxxxxx tmp]$ awk -F "," '{print $1}' /tmp/users_in.list | awk '{print $1}' > last.name
[ngreenfield@xxxxxx tmp]$ paste first.initial last.name | awk '{print $1$2}'
AAtun
JBatrez
SBevz
RCantley
TCheng
KChow
TCronk
JElizalde
KFindlay
CFrary
WHall
JHuang
AHunt
KJeffries
JKubiak
TMelton
CMolloy
ANg
SPatel
JPuckett
BRhoades
ARuiz
JSchmitt
TSenfleben
SSpangler
MSteingrebe
JTanner
DTrinh
JWalters
GWendover
DWright
PYara
EAcquisto
SBohannon
MChurchill

For some reason though I can't do this with variables:

Code:
[ngreenfield@xxxxxx tmp]$ FIRSTINITIAL=`cat /tmp/users_in.list | awk -F "," '{print $2}' | cut -c -1`
[ngreenfield@xxxxxx tmp]$ LASTNAME=`awk -F "," '{print $1}' /tmp/users_in.list | awk '{print $1}'`
[ngreenfield@xxxxxx tmp]$ paste $FIRSTINITIAL $LASTNAME | awk '{print $1$2}'
paste: A: No such file or directory
[ngreenfield@pdtscope003 tmp]$

Any ideas?
# 2  
Old 10-01-2013
The paste command is for files.

By the way, there are many simpler ways to do what you are attempting to do - since only using one file. Or, maybe that was simply your example.
# 3  
Old 10-01-2013
Joey,

Basically I am given a list of name phonebook style:

Otun,Bill A
Bald,Jared P
Berch,Sarah O
Connor,Randy J
Church,Tom

I want to be able to output them as firstinitial.lastname format, like:

botun
jbald
sberch
rconnor
tchurch

So what would you recommend as the best way to resolve this? I had been using awks to get a list of first initial then lastname while filtering out the middle initial and trying to then mash them together.

Thanks,

Nick
# 4  
Old 10-01-2013
Code:
awk -F, '{print substr ($2,1,1)$1}' file
BOtun
JBald
SBerch
RConnor
TChurch

or
Code:
awk -F, '{print tolower(substr ($2,1,1)$1)}' file
botun
jbald
sberch
rconnor
tchurch

This User Gave Thanks to RudiC For This Post:
# 5  
Old 10-01-2013
Another version in Perl:

Code:
perl -ne 'print "\L${2}\L${1}\E\n" if(/(\w+),(\w).*/)' file
botun
jbald
sberch
rconnor
tchurch

# 6  
Old 10-01-2013
Rudi,

Thanks, thats exactly what I'm looking for!

-Nick
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with paste command

Hi, I am facing issue with paste command. It is adding spaces or tab in between. I have say 3 files with below data File_1 TH THI THIS I File_2 IS IS S IS RE S File_3 RECORD 1 CORD 2 IS RECORD 3 (3 Replies)
Discussion started by: Simanto
3 Replies

2. Shell Programming and Scripting

Problems with awk (fatal error) and paste (two variables into one column-by-column)

Hello, I have a script extracting columns of useful numbers from a data file, and manipulating the numbers with awk commands. I have problems with my script... 1. There are two lines assigning numbers to $BaseForAveraging. If I use the commented line (the first one) and let the second one... (9 Replies)
Discussion started by: vgbraymond
9 Replies

3. Shell Programming and Scripting

How do I use paste command in while condition??

there are lot of files where in mostly all the file contains 2columnsAl,1 Ail,13 Al,3 Al,1 Al,3 Al,2 Al,3 Al,1 Al,1 Al,1 My requirement is i wanted only the second column of every file as a column ony in the base file... I used paste command... but it is not helping as I'm using while... (7 Replies)
Discussion started by: nikhil jain
7 Replies

4. Shell Programming and Scripting

echo two variables like the paste command is not working

Dear all, I have two files like this file1 A B C D E F file2 1,2 3,4 5,6 I want this output output_expected A B 1,2 C D 3,4 E F 5,6 (3 Replies)
Discussion started by: valente
3 Replies

5. Shell Programming and Scripting

Can't paste in command line.

Hello. I've made a simple script which asks the user to input a hash and then runs a command that replaces the variable $hash with what the user inserted. The ting is that when the programm asks for input I can't paste anything there..! any clues?? :wall: (8 Replies)
Discussion started by: louboulos
8 Replies

6. Shell Programming and Scripting

Bash-how to properly READ and PASTE variables.

Recently i made a script for a project at molecular dynamics but am stuck at the last step.The thing i want to do is to ask the user to input the number of particles, then replace the bolded numbers at lines 9 and 17.. code #!/bin/bash #read number of particles echo "insert the number of... (2 Replies)
Discussion started by: arisinhell
2 Replies

7. UNIX for Dummies Questions & Answers

Need help with using cut and paste command

I have a file which contains 3 fields separated by tabs example andrew kid baker I need to swap kid and baker using cut and paste commands how is this to be done? Thanks (1 Reply)
Discussion started by: drew211
1 Replies

8. UNIX for Dummies Questions & Answers

paste command

input1 15 150 input2 x 10 100 input3 y 20 200 z 34 44 cmd paste -d "\t" input1 input2 input3 >>output output (1 Reply)
Discussion started by: repinementer
1 Replies

9. Shell Programming and Scripting

command paste with variables

Hi. I have an interesting problem and i couldn't find out the solution. I have two variables in which there are a lot of lines finished by \n. I would like to concatenate this two variables into one in this format: var1var2 var1var2 . . . I could do this simply by command paste but it works... (32 Replies)
Discussion started by: samos
32 Replies

10. UNIX for Advanced & Expert Users

paste command

I wonder if any body can help me with a command i am struggling with. I have a file with around 400 lines in, in a program i have it pulls out each line at a time so that data from the line can be cross referenced with another file. If it finds a match it pulls out a ocde from the second file, this... (5 Replies)
Discussion started by: mariner
5 Replies
Login or Register to Ask a Question