asked    Lee     2018-10-22       bash       121 view        1 Answer

[SOLVED] While Read Line - Limit Number of Lines

I am trying to limit the number of lines found during a while read line loop. For example:

File: order.csv

 123456,ORDER1,NEW
 123456,ORDER-2,NEW
 123456,ORDER-3,SHIPPED

I am doing the following.

 cat order.csv | while read line;
 do
  order=$(echo $line | cut -d "," -f 1)
  status=$(echo $line | cut -d "," -f 3)
  echo "$order:$status"
 done

Which outputs:

  123456:NEW
  123456:NEW
  123456:SHIPPED

How can I limit the number of lines. In this case there are three. How can I limit them to only 2 so that only the first two are displayed?

Desired output:

  123456:NEW
  123456:NEW

  1 Answer  

        answered    Cash     2018-10-22      

There are some ways to meet your requirements:

Method 1

Use head to display first few lines of a file.

head -n 2 order.csv | while read line;
do
    order=$(echo $line | cut -d "," -f 1)
    status=$(echo $line | cut -d "," -f 3)
    echo "$order:$status"
done

Method 2

Use a for loop.

for i in {1..2}
do
    read line
    order=$(echo $line | cut -d "," -f 1)
    status=$(echo $line | cut -d "," -f 3)
    echo "$order:$status"
done < order.csv

Method 3

Use awk.

awk -F, 'NR <= 2 { print $1":"$3 }' order.csv




Your Answer





 2018-10-22         Noah

SQL - using calculated dates in WHERE clause

I have a table with future values where I only want the last full week. Meaning af want to find the last Sunday and have a WHERE clause where date is between 'last Sunday - 7' and 'last Sunday'. Meaning I have transactions until '31-10-2018' and want the period between '2018-10-28' and '2018-10-22' (both days included). But every week new data will be added continuously so very soon I have data until '2018-11-30' and so on.I have tried a query like:SELECT *FROM PlannedCountsWHERE [Date] BETWEEN DATEADD(d,-6,MAX([Date])) and MAX([Date])Note: I'm aware that this will only giv...
 sql                     2 answers                     61 view
 2018-10-22         Marcus

Sqlite getting the latest record for this table

I can't figure out what I am doing wrong here. I am querying sqlite table the to get the latest record for each College but the result is not completely accurate. Below is the source table:College Status Date"UTAR "CERTIFIED" "07/30/2018""UTA" "CERTIFIED" "03/19/2018""UTD" "CERTIFIED" "06/08/2018""UTEL" "CERTIFIED" "03/13/2018""UTSA" "CERTIFIED" "02/06/2018""UTT" "CERTIFIED" "03/14/2018""UTPB" "CERTIFIED" "07/23/2018""UTRG" "CERTIFIED" "04/12/2018""UTAR" "CERTIFIED" "05/31/2017""UTA" "CERTIFIED" "03/02/2017""UTD" "CERTIFIED" "04/13/2017""UTEL" "...
 sql                     2 answers                     108 view
 2018-10-22         Kyle

How to rewrite implicit joins to explicit joins?

I'm having some trouble re-writing these JOINS using explicit joins. I'm trying to understand how this is accomplished. Here is the original query using implicit joins that I've inherited:FROM prod.f31122 t, prod.f4801 wo, prod.f30008 r, prod.f0101 abWHERE t.wtdoco = wo.wadoco(+) and t.wtmmcu = wo.wammcu(+) and t.wtmmcu = r.iwmcu and t.wtan8 = ab.aban8I've tried the following, but I'm getting different result sets. The same count, but the result set is incorrect (likely an issue with my groupings which aren't displayed). Here is my translati...
 sql                     3 answers                     111 view