cat command not displaying file contents with begin with + sign - shell

I have created a file with contents as given below.
+---------------------------+
| Tasks
+---------------------------+
| 1) option one
| 2) option two
+---------------------------+
But it is not displaying anything while I am trying to run the following command
cat < filename
Please help me to solve this.
Thanks in advance.

Use cat filename. The redirection isn't necessary.
(You could do cat - < filename also).

Related

How do I open a file in VS Code terminal by partially matching the file name?

If I have a file named w5_align_example.cpp, how do I open that file in VS Code integrated terminal by only supplying the word align?
code w5_align_sample.cpp would open it but I sometimes only remember the keyword align unless I search in a separate command to see what the file begins with. I want to open in a single command instead.
I've tried:
$ ls | grep "align" | code which gives me Run with 'code -' to read output from another program (e.g. 'echo Hello World | code -'). error.
$ ls | grep "align" | code - opens up a new file called code-stdin-sfd.txt with the text w5_align_example.cpp inside.
What would be the simplest (i.e. shortest) command to do this?
ls | grep "align" | xargs -I{} code {}
or
code $(ls | grep "align")
You can just use *. It matches any string and can be used multiple times.
code *align*
In some shells, you can combine this with tab completion. Just type:
code *align*
And then press Tab. This will fill in the rest of the file name, but it will beep if there is more than one option.

Append data to the end of a specific line in text file

I admit to being a novice in bash script, but can't quite seem to figure out how to accomplish a key step in a script and couldn't quite find what I was looking for in other threads.
I am trying to extract some specific data (numerical values) from multiple .xml files and add those to a space or tab delimited text file. The files will be generated over time so I need a way to append a new dataset to the pre-existing text file.
For instance, I would like to extract values for 3 different categories, 1 per row or column, and the value for each category from multiple xml files. Basically, I want to build a continuous graph of the data from each of 3 categories over time.
I have the following code which will successfully extract the 3 numbers from the xml file and trim the unnecessary text:
#!/bin/sh
grep "<observation name=\"meanGhost\" type=\"float\">" "/Users/Erik/MRI/PHANTOM/2/phantom_qa/summaryQA.xml" \
| sed 's/<observation name=\"meanGhost\" type=\"float\">//g' \
| sed 's/<\/observation>//g' >> $HOME/Desktop/testxml.txt
grep "<observation name=\"meanBrightGhost\" type=\"float\">" "/Users/Erik/MRI/PHANTOM/2/phantom_qa/summaryQA.xml" \
| sed 's/<observation name=\"meanBrightGhost\" type=\"float\">//g' \
| sed 's/<\/observation>//g' >> $HOME/Desktop/testxml.txt
grep "<observation name=\"std\" type=\"float\">" "/Users/Erik/MRI/PHANTOM/2/phantom_qa/summaryQA.xml" \
| sed 's/<observation name=\"std\" type=\"float\">//g' \
| sed 's/<\/observation>//g' >> $HOME/Desktop/testxml.txt
This gives the output:
1.12
0.33
134.1
I would like to then read in another xml file to get:
1.12 1.45
0.33 0.54
134.1 144.1
I would be grateful for any help with doing this! Thanks in advance.
Erik
It's much safer to use proper XML handling tools. For example, in xsh, you can write something like
$f1 := open /Users/Erik/MRI/PHANTOM/2/phantom_qa/summaryQA.xml ;
$f2 := open /path/to/the/second/file.xml ;
echo ($f1 | $f2)//observation[#name="meanGhost"] ;
echo ($f1 | $f2)//observation[#name="meanBrightGhost"] ;
echo ($f1 | $f2)//observation[#name="std"] ;

this one-line shell script not working on Mac - any ideas how to fix?

This unix command I haven't got quite working on Mac yet - any ideas what needs adjusting:
find . | grep '.*\(css\|js\|rjs\|rhtml\|rb\)$' | sort | while read in; do printf "\n\n####\n# FILE: %s\n####\n\n" ${in} >> onebigfile; cat "${in}" >> onebigfile; done
thanks
The purpose of this command is to gather the content of all the files under the current directory whose names ends as said (css ... rb) in a file named onebigfile (with delimiters) IIUC.
To debug this type of series of piped commands, you can run the individual commands, or individual groups of commands to try to see what is happening. For instance, try:
find .
find . | grep '.*\(css\|js\|rjs\|rhtml\|rb\)$'
find . | grep '.*\(css\|js\|rjs\|rhtml\|rb\)$' | sort
Then get one line of the output (for example ./dir/file.css), and try:
echo './dir/file.css' | while read in; do echo ${in}; done
echo './dir/file.css' | while read in; do cat ${in}; done
echo './dir/file.css' | while read in; do cat ${in} >> onebigfile; done
You should bo able then to understand what's happening.
The problem may be due to file and directory names containing spaces. The solution in this case is to use find -print0 command.

Problem with including an "image" in shell program

I'm writing a program where at some point in my loop I want to print to output whatever is stored in a separate file (an image). But if I code it like this:
for c in $LIST
do
clear
./image.0
done
And the "image.0" file contains only an image like this:
+----+
| |
|
|
|
|
|
========
Then when I run my program I get this message:
./image.0: 1: +----+: not found
./image.0: 2: Syntax error: "|" unexpected
Why?
================================
So "cat" works, the image appears in the output but it's shifted in a strange way.
Do you know why this would happen?
+----+
| |
|
|
|
|
|
========
Answer: I put printf "\n" that fixed the shifting image
With ./image.0, you tell the shell to execute the image. You want to output it, so use cat image.0
Try to use the command cat to output the content of the image.0 file
cat ./image.0
./something will take something as a program and execute it. That's not what you want : to display the contents of a file, you can use the cat command, like this :
for c in $LIST
do
clear
cat image.0
done

How do I append onto pipes?

So my question is if I can somehow send data to my program and then send the same data AND its result to another program without having to create a temporary file (in my case ouputdata.txt).
Preferably using linux pipes/bash.
I currently do the following:
cat inputdata.txt | ./MyProg > outputdata.txt
cat inputdata.txt outputdata.txt | ./MyProg2
Here is another way, which can be extended to put the output of two programs together:
( Prog1; Prog2; Prog3; ... ) | ProgN
That at least works in Bash.
Choice 1 - fix MyProg to write the merged output from the input and it's own output. Then you can do this.
./MyProg <inputdata.txt | ./MyProg2
Choice 2 - If you can't fix MyProg to write both input and output, you need to merge.
./MyProg <inputdata.txt | cat inputdata.txt - | ./MyProg2

Resources