awk not outputting properly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk not outputting properly
# 8  
Old 04-30-2012
well here is what I got and I think maybe the problem is my text file I was given has carriage returns in it? I am not sure of the source of it's creation as it probably changed hands half a dozen times until coming down the pipe to me.

My script basically looks like this now:

Code:
#!/bin/bash

# generate list of all folders that need to be renamed

fileList=$(/bin/ls -l /path/to/a/bunch/of/folders)

# input file of old,new folder names

folderNames="/path/to/folders_list.txt"

/bin/echo ${folderNames}

# loop through fileList and move home folder 

for i in ${fileList} ; do

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


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

  then /bin/echo "${i} needs to be moved to ${newFolder}"
          /bin/echo "place holder for work flow mv of commands..."
  
  else /bin/echo "${i} does not have a match"
  
fi
done

exit 0

So, in my testing, everything outputs when ran from sh -x /path/to/script.sh or bash -x, except the newFolder variable will never output properly. Now that I think about it, I think the source file containing the old,new names could have been created on a Windows box, and was definitely created in Microsoft Office.

I haven't tried running the script on any machine just yet because of the echo output not matching.

Any ideas? Sorry, I am not the strongest awk programmer.
# 9  
Old 04-30-2012
Do you really need to check the file and the directory?

Code:
while IFS=, read -r olddir newdir; do
    newdir=${newdir%$'\r'} #remove dos line endings...?

    echo mv "$olddir" "$newdir"
done < folder_list.txt

or using globs and than awk:
Code:
for dir in /path/to/bunch/of/folders/*; do
    [[ -d $dir ]] || continue

    newFolder=$(awk -F, -v "olddir=$dir" '$1 == olddir {sub(/\r/,"");print $2;exit}')

    if [[ $newFolder ]]; then
        echo "got to move $dir to $newFolder"
    else
        echo "no match: $dir"
    fi
done

untested quick reply...
# 10  
Old 04-30-2012
Yeah the file contains the list of the new standard naming convention for shared folders on the network. Basically, I inherited other sys admin's servers and now am merging remote offices and trying to enforce standards put forth by upper management.

They supply me a csv file of a list of all the old folders, and then what the new folder name should be in the column next to it. During my troubleshooting to get that newFolder to echo out right I just dumped everything in a plain text file, which is comma delimited.

Sorry if this isn't making much sense, I can use awk, but am not a grand master at it.
# 11  
Old 04-30-2012
Try using dos2unix to convert your data file, then process it with your script.
# 12  
Old 04-30-2012
Quote:
Originally Posted by ThomasMcA
Try using dos2unix to convert your data file, then process it with your script.
Well is that a reason why my echo output would come up blank?
# 13  
Old 04-30-2012
Then I don't see much issue in trying the first code snipplet. It works here:

Code:
mute@goflex:~/test$ ls
folder_list.txt  olddir  olddir2  olddir3  script
mute@goflex:~/test$ ./script
mute@goflex:~/test$ ls
Projects  Something Awesome  Web Site  folder_list.txt  script
mute@goflex:~/test$ cat folder_list.txt
olddir,Something Awesome
olddir2,Projects
olddir3,Web Site
mute@goflex:~/test$ cat script
#!/bin/bash
while IFS=, read -r olddir newdir; do
        newdir=${newdir%$'\r'} #remove dos line endings...?

        mv "$olddir" "$newdir"
done < folder_list.txt
mute@goflex:~/test$

# 14  
Old 05-01-2012
Quote:
Originally Posted by neutronscott
Then I don't see much issue in trying the first code snipplet. It works here:

Code:
mute@goflex:~/test$ ls
folder_list.txt  olddir  olddir2  olddir3  script
mute@goflex:~/test$ ./script
mute@goflex:~/test$ ls
Projects  Something Awesome  Web Site  folder_list.txt  script
mute@goflex:~/test$ cat folder_list.txt
olddir,Something Awesome
olddir2,Projects
olddir3,Web Site
mute@goflex:~/test$ cat script
#!/bin/bash
while IFS=, read -r olddir newdir; do
        newdir=${newdir%$'\r'} #remove dos line endings...?

        mv "$olddir" "$newdir"
done < folder_list.txt
mute@goflex:~/test$

I think this may not work, since there is one master list and a ton of shares, and the file shares will have tons of different sets of folders. To give a broad example, some offices will have say an accounting department with different folders, and other offices won't. So, I need to test whole file shares against a master naming convention list.

Does that make sense? Will this work?

See when I do this:

Code:
awk -F, '/testfolder1/ { sub(/\r/,"");print $2;exit }' ~/home/folders_list.txt 
testfolder2

So, I have a variable that grabs the new folder by comparing the folder name in the loop to the master text list, if it finds it, it prints the second value delimited by comma. I am sure I am not coding this the most efficient way, but I am not a total awk wizard here, just know enough to get jobs done. So, my variable is:

Code:
newFolder=`awk -F, '/t${i}/ { sub(/\r/,"");print $2;exit }' ~/home/folders_list.txt `
echo ${newFolder}
''

So, when I echo out the output of the newFolder it always turns up blank when I test it.

See that output works like a charm when I run it manually. When I pass my array of data through a loop and use ${i} in place of testfolder1 and run the script via bash -x /path/to/script, the output is blank. It outputs nothing. When I run the command manually, it outputs testfolder2. In my text file somewhere in the middle there is this entry:

testfolder1,testfolder2

So, is there any valid reasons why my echo will not output?

Last edited by Zaphod_B; 05-01-2012 at 11:03 AM..
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