Including a space taken from string in a var
I have a tsv file that I'm trying to break down, line by line, then column by column so to speak. The file is a list of exported usernames, passwords, and display names from an email server, I need each separate to be used in other commands to provision the new server. My problem is, each display name contains a space, as usual with people's names.
Here is the script for reference: Code:
while read line Code:
[email][tab][password][tab][display name] I've tried: Code:
display=`echo $line | awk '{ print $3 }' >> works` #of course, I just get the first name |
Quote:
Code:
jdoe@example.com test123 John Doe |
Assuming tabs as separators, this should work:
Code:
while read line Also, use $(command) instead of backticks. It's easy to nest and can't be confused with quotes. |
Hi,
try using the tab as field separator in awk Code:
awk -F '\t' '{print $3}' |
Quote:
Quote:
Quote:
|
Quote:
Code:
while read line |
Just to be clear, what shell are we talking about here? Is it bash, another shell, or do you need this to be posix-compliant? It may affect what commands can be used.
If you're using bash or another bourne-based shell, then you can probably skip awk entirely and use some other technique instead. bash's read command can be used to set arrays, for example, with IFS controlling the delimiter: Code:
IFS=$'\t' |
Also, I just noticed this:
Code:
uname=`echo $line | awk '{ print $1 }' >> works` Set the variable first, then echo it into the file with a separate command. |
Quote:
Quote:
|
read is a bash built-in keyword, so it's found in the bash man page. You can also get a quick description with "help read". The man page you're reading is likely from a separate application of the same name. The man page for read on my system is for the C library function.
I said awk because that's what the last post was talking about when I started typing, and I didn't see yours until after I posted. But since cut is also an external program, my comment applies to it as well; this can be done entirely in-shell, no external tools necessary. :) |
Quote:
Quote:
|
True, cut is a lighter and more efficient choice than awk for something like this.
But when it comes to scripting input that's field-delimited, or otherwise should be treated as separate-but-related strings, arrays are the first thing you should turn to. The other tools are mostly there for getting the data into the array. ;) |
Quote:
This Code:
while read line; do echo "${line}" | awk -F '\t' '{print $3}'; done < /path/to/file If you do not need to do more complex String manipulations than in your example then I would also recommend that you use David's solution from post #7. |
Quote:
Once this is all settled, I have to issue a command like: Code:
provision $uname $pass displayName $name Now, I have to examine $pass to see if it's less than 6 characters. Any recommendations? |
Quote:
Code:
chars=$(echo $pass | wc -c) |
All times are GMT -5. The time now is 11:12 AM. |