0

I have a file with numbers in like this:

1234 5678 3456 

How can I get this on one line using a bash shell script with a space between each like this

1234 5678 3456 
1
  • Do you know it's going to be three lines in your file, or may there be a thousand or more lines? Commented Jul 15, 2021 at 15:44

2 Answers 2

4

The easiest way is to use xargs. e.g.

xargs < file.txt 
6
  • 1
    well, it'll only work if all the numbers will fit into one shell command line. I don't know what the line length limit is on hp-ux, but on linux it's about 2 million characters. If there's more than will fit in one line, xargs will print a second (or third...) line. Commented Jul 15, 2021 at 15:54
  • Daft question - the max there will be is 8 sets of numbers. I then want to read the file.txt and make that into a variable called numbers so that when numbers is called it contains 1234 5678 3456. I know there will be some quoting cus of spaces but unsure how to do it. Commented Jul 16, 2021 at 7:05
  • numbers="$(xargs < file.txt)" Commented Jul 16, 2021 at 7:21
  • I'm also trying work out a way that when I read a users input for the numbers that I don't output letters, a number below 1000 or over 9999 to the file im using. I have this but I don't think its right. Commented Jul 16, 2021 at 14:02
  • # Loop to check the numbers entered by user are valid and outputs them # to a file called numbers.txt # The loop will only let you enter numbers from 1000 to 9999 x=0 until [ ! $x -ne $br2update ] do echo "Enter number" read store if [ $store -gt 1000 ] && [ $store -lt 9999 ] then echo $store >> numbers.txt x=echo "$x + 1" | bc else echo "Invalid number" fi Commented Jul 16, 2021 at 14:44
2

‍‍‍‍‍‍‍‍‍‍‍‍

paste -sd' ' file 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.