Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash how to print for loop output into one line?

Tags:

bash

mysql

awk

perl

I wish if someone can help me with this,

how can I print the output of for loop into single line?

for i in `cat file.csv`
do
echo $i 
done

what I'm trying to achieve here is to get a list of numbers from file.csv to generate a mysql bulk delete statement.

delete FROM Recordtable WHERE DataID IN ('93041', '93031' ...etc);

and the goal here is to load every 1000 records into one single delete statement

Your help is highly appreciated

like image 898
Deano Avatar asked Jan 21 '26 14:01

Deano


1 Answers

If you want the loop, you can use

for i in ($<file.csv)
do
  echo -n "$i "
done

The -n option to echo suppresses the newline.


You can print the entire file in a single line with echo $(<file.csv).

But this may suit your needs better:

awk '{ printf $0 " " } NR%1000 == 0 { print "" }' file

This will print each line followed by a space. It will print a newline whenever the line number is divisible by 1000; that is, after every 1000th line.


Added: To print each line within parentheses, you can use

awk '{ printf $0 " " } NR%1000 == 0 { print "" }' file | sed 's/.*/(&)/'

The sed command searches for any characters (.*) and replaces them with an open-paren, the characters it found (that is, the entire line), and a close-paren.

like image 66
Adam Liss Avatar answered Jan 23 '26 08:01

Adam Liss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!