awk not outputting properly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk not outputting properly
# 1  
Old 04-21-2012
awk not outputting properly

Hi Everyone,

Long time lurker here. I have a project of bringing every one of our data centers to a newly enforced company standard. Standard naming conventions, domain migrations, etc. So, the people who are setting the standards are providing me with a CSV file. Column 1 has the old value, Column 2 has the new value. It looks like this example I pulled off the Internet:

Code:
President ,Home State
George Washington,Virginia
John Adams,Massachusetts
Thomas Jefferson,Virginia
James Madison,Virginia
James Monroe,Virginia
John Quincy Adams,Massachusetts
Andrew Jackson,Tennessee
Martin Van Buren,New York
William Henry Harrison,Ohio
John Tyler,Virginia
James K. Polk,Tennessee
Zachary Taylor,Louisiana
Millard Fillmore,New York
Franklin Pierce,New Hampshire
James Buchanan,Pennsylvania
Abraham Lincoln,Illinois
Andrew Johnson,Tennessee
Ulysses S. Grant,Ohio
Rutherford B. Hayes,Ohio
James A. Garfield,Ohio
Chester A. Arthur,New York
Grover Cleveland,New York
Benjamin Harrison,Indiana
Grover Cleveland (2nd term),New York
William McKinley,Ohio
Theodore Roosevelt,New York
William Howard Taft,Ohio
Woodrow Wilson,New Jersey
Warren G. Harding,Ohio
Calvin Coolidge,Massachusetts
Herbert Hoover,Iowa
Franklin D. Roosevelt,New York
Harry S. Truman,Missouri
Dwight D. Eisenhower,Texas
John F. Kennedy,Massachusetts
Lyndon B. Johnson,Texas
Richard Nixon,California
Gerald Ford,Michigan
Jimmy Carter,Georgia
Ronald Reagan,California
George H. W. Bush,Texas
Bill Clinton,Arkansas
George W. Bush,Texas
Barack Obama,Illinois

I am using awk to parse through and find the desired value of column one and then have it print the value right behind it. Here is my script but I cannot for the life of me to get one variable to output properly.

Code:
#!/bin/bash

# generate array of old file/folder/etc names

fileList=`commands to build array of data` # for arguments sake the presidents CSV file printed above

oldFile="/path/to/my/file_list.txt" # source of text file with old and new values

# loop through fileList and move folders or files

for i in ${fileList} ; do

newFile=$(/usr/bin/awk -F, '/${i}/ { print $2;exit }' ${oldFile})

if [[ `/usr/bin/awk -F, '/${i}/ { print $2;exit }' ${oldFile}` == ${newFile} ]]

  then /bin/echo "${i} needs to be moved to ${newFile}"
         /bin/echo "bunch of commands to rename folders"
  
else /bin/echo "${i} does not have a match"
  
fi
done

exit 0

Sorry for being really vague but trying to get a proof of concept here with out giving out any confidential information that may or may not be on the actual real scripts I am writing. Basically, it seems to work but when I run the script via bash -x /path/to/script the newFile never outputs right at all.
# 2  
Old 04-21-2012
The most useful info for people here to help you is input and desired output, otherwise it will take a long time for people to figure out what exactly you want before they start thinking of a solution to your problem.
# 3  
Old 04-22-2012
It doesn't make much sense you assign newFile to some awk command, then check if that same command is equal to the previous answer?

Also, when you run under -x do you notice it trying to match ${i} instead of the value inside of $i? awk is a separate program, it doesn't know your shell variables unless you pass them. Also it will be treated as a regex in that context so a dot will match any character.

Code:
for file in ${fileList}; do
   newFile=$(awk -F, -v "file=$file" '$1 == file { print $2; exit }' ${oldFile})

This User Gave Thanks to neutronscott For This Post:
# 4  
Old 04-22-2012
Very confusing. What I interpreted is:
1) you have a list of old to new translations in the form: <old>,<new>.
2) you have a list of existing files in the form: /some/path/file-name
3) filenames can have embedded spaces
4) you need to take /some/path/file-name and convert it to /some/path/new-name and then execute the move command do do the work.

Assuming the above list, here is a skeleton script assuming that the translations is in xlate.list and the existing files are in exist.list.

Code:
#!/usr/bin/ksh
typeset -A xlate
while IFS="," read oldv newv        # build translation from csv file
do
    xlate["$oldv"]="$newv"
done <xlate.list

# read list of existing files/directories
while read existing
do
    basename=${existing##*/}
    if [[ -n ${xlate[$basename]} ]]
    then
        echo "move '$existing' to '${xlate[$basename]}/${existing%/}'"
        # command to move the file
    else
        echo "existing file has no new value: '$existing'"
    fi
done <exist.list  #</path/to/existing/list

# 5  
Old 04-22-2012
Sorry for being so confusing.

The /path/to/file will be where the comma delimited file lives. On it it will have an existing value, and the value next to it that it should be changed to. So for example, there will be entries like this on the source file:

oldvalue1,newvalue1
oldvalue2,newvalue2
oldvalue3,newvalue3

So, I am basically creating an array of folders by either the find command or ls command depending on the task. I was not the original administrator of any of these servers or file shares. Then checking my comma delimited source file and if it finds a match in the first value, it will rename it via the mv command to the new standardized naming conventions.

My only hang up with my script is I cannot get awk to output one command properly. The thing that kills me, is that it works fine in the terminal, but when I put it in a script it just has a blank output. So if I run this in terminal:

Code:
/usr/bin/awk -F, '/oldvalue1/ { print $2;exit }' /path/to/input/file

that works and it outputs newvalue1, going off the example I gave earlier. However, when I put that command in my script and use variables, it doesn't seem to output properly. I tested my output by doing this:

bash -x /path/to/script.sh and every line prints out, there are no errors, it works as intended, but I am not seeing that output when I do that. It outputs a blank value. This is what is totally baffling me.

Sorry if I am still being confusing.
# 6  
Old 04-22-2012
I think you could do things more efficiently than executing an awk to read your CSV file for every old file. However, working with what you've given, you could try this:

Code:
/usr/bin/awk -F, 'match( $1, ov ) { print $2; exit }'  ov=$oldvalue /path/to/input/file

The assumption is that the shell variable oldvalue has the name of the file that your script is trying to find in the /path/to/input/file file.

Last edited by agama; 04-22-2012 at 08:18 PM.. Reason: typo
# 7  
Old 04-22-2012
Thanks, I need to sit down and read the book on awk, but never have the time. I will try this when I get to my hotel room. Currently traveling and on my hotspot. I will need to VPN to grab all my resources needed to test this.

Thanks,
ZB
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk print not working properly

Hello friends, There is one requirment where I need to login into database environment and pull all schema names into a text file ... as of now below are the schemas available... $> describe keyspaces; system_schema system_auth system abc system_distributed system_traces Now from... (4 Replies)
Discussion started by: onenessboy
4 Replies

2. Shell Programming and Scripting

Problem outputting increment

With this script the output to the terminal does not increment. Can anyone tell me what I need to do to get this to increment output to the terminal? Here is the output mpath major,minor number ls: /dev/mapper/mpathp1: No such file or directory raw device output 253,44 echo raw device... (5 Replies)
Discussion started by: bash_in_my_head
5 Replies

3. Shell Programming and Scripting

awk, sed, perl assistance in outputting formatted file

Hello, Please advise. Scoured this site, as well as google for answers. However if you do not know what to search for, it's a bit hard to find answers. INPUT: ACTASS= 802 BASECOS= 279 COSNCHG= 3 CUSCOS= 52 UPLDCOS= 2 DESIRED OUTPUT: ACTASS=802 BASECOS=279 (13 Replies)
Discussion started by: abacus
13 Replies

4. Shell Programming and Scripting

AWK how to change delimiter while outputting

Hello I need some help in outputting Fields when the delimiter has changed: echo "test1,test2 | test3,test4,test5" | awk -F"," '{print $1,"COUNT",$2,$4}' prints out: test1 COUNT test2 | test3 test5 But how to change the -F"," to -F"|" delimiter, so that it separates the fields from $2... (2 Replies)
Discussion started by: sdohn
2 Replies

5. Shell Programming and Scripting

AWK: pattern not properly stored in variable?

Hey there, I have a table of contents file of the form 1 Title1 1.1 Subtitle1 1.1.1 Subsubtitle1 1.1.2 Subsubtitle2 ... and want to count the number of dots in the first field to find out the level of the section. I use the gsub function for the job, which works if I pass the pattern... (2 Replies)
Discussion started by: herrsimon
2 Replies

6. UNIX for Dummies Questions & Answers

PC awk not working properly on OSX

Hi, I'm having some trouble with an awk programme that i'm using to scan ascii files. Unfortunately I'm not an experienced programmer but I think I am experiencing problems for a two reasons: 1) the awk was written by a PC programmer and it works on his machine, but only partly works... (10 Replies)
Discussion started by: Dan Browne
10 Replies

7. Shell Programming and Scripting

Script Assistance - Outputting to file with Awk

I'm trying to take a list of domains, find out the MX resolve it to IP then find out what the NS is and output the contents to a new file. The only problem i'm having is when checking the Ip or host of the MX i can only get it to print the column with the MX record and the results of the host... (1 Reply)
Discussion started by: spartan22
1 Replies

8. UNIX for Dummies Questions & Answers

getting input, then outputting it

Hi! I am a newbie to Unix. I was writing a little game program for fun when thought of an idea to allow data to be saved. I knew to take all of the Predefined variables and put them into a separate file, then including the file in the program. But I am having trouble making it so that the user... (0 Replies)
Discussion started by: signebedi
0 Replies

9. Shell Programming and Scripting

Outputting to table

I have information in a file called HITS. This file has been populated by the user entering search criteria. the HITS file contains information: filname.hits: 123.33.345.66 Fri Nov 26 11.45.56.43 GMT 2006 at the moment i am just displayin the information using cat HITS. ... (3 Replies)
Discussion started by: amatuer_lee_3
3 Replies

10. Shell Programming and Scripting

expr not outputting properly.. bsd sh script

When i run sh -x test.sh, expr outputs x=expr $x + 1 instead of doing the arithmetic.. been working on this overnight.. and its being a pain in the arse if you ask me.. :confused::confused: #!/bin/sh #script for downloading numerical filenames chap=1 p=1 count=0 x=1 while do if ... (2 Replies)
Discussion started by: aspect_p
2 Replies
Login or Register to Ask a Question