How can I read data separated by space in two columns and print value from column-1 that may contains Null or empty value next to it in columns-2 into a file, otherwise perform action if it has values in both columns:

data set:

User Numbers
u1 12   
u3 45
u3 20
u4 5
u6 7
u7 32
u9 10
u10 14

You can do it with awk, for example. The variables $1 and $2 contain the text of the columns, and NR contains the number of columns.

awk '
    NR == 1 {print "Only one column: " $1}
    NR == 2 {print "The sum of the two columns is", $1 + $2}

Thanks bunch for your answer... I tried your answer and some part did work... except the Column-1 was showing incorrect User for value in column-2. I ended up putting my head around it and created something like this:


while IFS= read -a line; do COL1=$(printf "%s\n" "${line[@]}" | awk '{print $1}') COL2=$(printf "%s\n" "${line[@]}" | awk '{print $2}') SRCH1=$(ldapsearch "(uid=${COL1})" | grep uid: | awk '{print $2}') SRCH2=$(ldapsearch "(uid=${COL1})" | grep initials: | awk '{print $2}') SRCH3=${SRCH2} if [ "$COL1" = "$SRCH1" -a "$COL2" = "$SRCH2" ]; then echo "UID: $SRCH1" echo "INITIALS: $SRCH2" else echo "NO UID: $COL1" echo "NO INITIALS: $COL2" fi done < datafile8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.