OSX paste command not pasting whole line [duplicate] - bash

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 1 year ago.
I am trying to paste two files together
file ip.txt
10.32.216.15
10.23.134.8
10.33.2.37
10.33.84.20
10.33.17.38
file obj.txt
obj-10.32.216.15
obj-10.23.134.8
obj-10.33.2.37
obj-10.33.84.20
obj-10.33.17.38
and I use the command like this:
paste ip.txt obj.txt
However I get this truncated output:
10.32.21obj-10.32.216.15
10.23.13obj-10.23.134.8
10.33.2.obj-10.33.2.37
10.33.84obj-10.33.84.20
10.33.17obj-10.33.17.38
The two files are created by grep command in my script earlier. This behavior is also present when I used variables with the command.
Also when I try to specify a delimiter only the second file gets pasted.
Does anyone know what might be the issue? Thanks

The issue was with line endings, for some reason Windows were set up but Unix were needed.

Related

How can I simply write all console output of a command to a file, in the terminal? [duplicate]

This question already has answers here:
print the output of strace command in a text file
(1 answer)
How to redirect and append both standard output and standard error to a file with Bash
(8 answers)
Closed 1 year ago.
I am trying to send all printed output of a strace command to a file. straces have a lot of console output since they print all the system calls of a command.
I have tried the most popular ways that I can find or think of, such as command > file.txt which does print output but then the output is not written to a file, same with command | tee file.txt Output is always printed but never written to a file. Is it because there is too much output or something? The first command definitely suceeds.
But my real question is what is the best way to do this and how can I get it done?
Many many thanks,
Milan

Bash script for moving files: cannot stat [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 1 year ago.
I am trying to write a bash script that takes 2 inputs: a list of IDs and 2) a directory. The idea is that the script will move a subset of files corresponding to the list of IDs into a a new folder.
#! /usr/bin/bash
LIST_FILE=$(cat ${1}) # List of file IDs (example entry: 1.2345)
PATH=${2} # directory name (i.e group_1)
for i in ${LIST_FILE}
do
/usr/bin/mv /tmp/project/project_data/data_all/${i}.annotated.gz /tmp/project/project_data/${PATH}/
done
The script manages to loop ok, however I get the following error for each iteration:
/usr/bin/mv: cannot stat '/tmp/project/project_data/data_all/1.2345'$'\r''.annotated.gz': No such file or directory
It looks like the file name hasn't concatenated properly and I'm not sure why. I've tried researching the problem but I'm also quite new to bash and finding it hard to grasp the concept of the stat error. I appreciate any advice and possible solutions.
Thanks everyone.
I think that the file whose name you pass as the first argument to your script is in dos format instead of unix so you are getting extra \r characters in your file names.
You could change your third line to:
LIST_FILE=$(cat ${1}|tr -d '\r')
Bobby

BASH echo showing some unusual behaviour [duplicate]

This question already has answers here:
Strange "echo" behavior in shell script
(3 answers)
Variables overwriting text problem with "echo" in Bash
(2 answers)
Closed 3 years ago.
This is the simple command that is giving me problems -
echo hafsda sfsdfdsfs $ymn $ymx $range
The output of this command is coming -
2.568 sfsdfdsfs 86.72
Where ymn = 86.72 ymx = 89.28 and range = 2.56. This only happens when I am using variables. The following command works fine -
echo hafsda sfsdfdsfs 1 2 $range
Also, the same command (the first one) works fine if I try running it directly in the terminal. This is only happening is a script. I also tried to use printf but encountered similar results.
I don't even understand what to google for to resolve this. I am unable to understand what is happening at all. So, what is happening here? Is this reproducible or is this just some error on my system, and if it is, what might be the problem?
Your script probably has DOS-style CRLF line endings. I suspect you actually have ymn="86.72\r" ymx="89.28\r" and range="2.56\r". You can test this in your script with
echo hafsda sfsdfdsfs $ymn $ymx $range | od -c
You can fix your script with dos2unix or sed -i 's/\r$// script.sh`.
Make sure you change the settings of your text editor do use unix line endings.

Strip last line in shell [duplicate]

This question already has answers here:
Remove the last line from a file in Bash
(16 answers)
Closed 5 years ago.
How can I strip the last line off a text file with Unix tools? Action can take place in place, so probably a version of truncate would be appreciated.
I guess there should be a solution using grep, sed, or head or similar tools, but I couldn't figure out a concise version.
Input:
one
two
three
Output:
one
two
sed is your friend. This prints everything but not the last line of a given file.
$ sed \$d file

Error extension file when use grep to write file [duplicate]

This question already has an answer here:
Why would a correct shell script give a wrapped/truncated/corrupted error message? [duplicate]
(1 answer)
Closed 7 years ago.
I have 100 ".txt" files. Each file contains the data such as
File name Data
1.txt BAP1
2.txt UCHL1
3.txt ABC1234
Now I want to scan content of these files and write to txt file with condition that it contains my input string such as "BAP1". I used below coded but the output files have mistaken such as '1.txt .' I have no idea why the extension file has more dot in last file. Could you help me solve it? I am working in cygwin
#!/bin/sh
grep -w 'BAP1' *.txt>"1.txt"
grep -w 'UCHL1' *.txt>"2.txt"
Run dos2unix on your script, or otherwise tell your editor to save it as a UNIX text file.
Otherwise, your filenames will have carriage returns (aka $'\r') on the end of their names.

Resources