0

How do i iterate through a file and print the first word only. The line is colon separated. example root:01:02:toor

the file contains several lines. And this is what i've done so far but it does'nt work.

  FILE=$1 
 k=1
   while read line; do
       echo $1 | awk -F ':' 
 ((k++))
 done < $FILE

I'm not good with bash-scripting at all. So this is probably very trivial for one of you..

edit: variable k is to count the lines.

kirkegaard
  • 1,058
  • 2
  • 12
  • 32
  • Ok, i see my mistake now. I assumed i needed a loop to iterate the lines in the file. I can just use "cut -d: " as it is without the loop – kirkegaard Dec 15 '15 at 20:15

4 Answers4

4

Use cut:

cut -d: -f1 filename
  • -d specifies the delimiter
  • -f specifies the field(s) to keep

If you need to count the lines, just

count=$( wc -l < filename )
  • -l tells wc to count lines
choroba
  • 231,213
  • 25
  • 204
  • 289
3
awk -F: '{print $1}' FILENAME

That will print the first word when separated by colon. Is this what you are looking for?

To use a loop, you can do something like this:

$ cat test.txt
root:hello:1
user:bye:2

test.sh

#!/bin/bash

while IFS=':' read -r line || [[ -n $line ]]; do
    echo $line | awk -F: '{print $1}'
done < test.txt

Example of reading line by line in bash: Read a file line by line assigning the value to a variable

Result:

$ ./test.sh
root
user
Community
  • 1
  • 1
zedfoxus
  • 35,121
  • 5
  • 64
  • 63
  • I'm supposed to make a script. So i need to have a loop. I dont know if i need to use awk at all, are there any better solutions for this problem? – kirkegaard Dec 15 '15 at 20:02
1

A solution using perl

%> perl -F: -ane 'print "$F[0]\n";'  [file(s)]

change the "\n" to " " if you don't want a new line printed.

DannyK
  • 1,342
  • 16
  • 23
1

You can get the first word without any external commands in bash like so:

printf '%s' "${line%%:*}"

which will access the variable named line and delete everything that matches the glob :* and do so greedily, so as close to the front (that's the %% instead of a single %).

Though with this solution you do need to do the loop yourself. If this is the only thing you want to do with the variable the cut solution is better so you don't have to do the file iteration yourself.

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
  • in the comment i wrote under my question. Can you answer why "cut-command" extracts the first word in each line without a loop? my intuition tells me that it only "reads" the first line.. – kirkegaard Dec 15 '15 at 20:17
  • Nope, it will apply the rules you give it to every line in the file. From `man cut`: "Print selected parts of lines from each FILE to standard output." Many commands will operate on files, so it's not nearly as commonly needed as done that people have to loop over the lines of a file themselves. Indeed, much of the time commands are actually designed to work on a file and we have to "trick" them to do a single line by piping it and having it read from the "file" `stdin` – Eric Renouf Dec 15 '15 at 20:20
  • Thank you! Good explanation. – kirkegaard Dec 15 '15 at 20:21