Paul Sutton

wc

Bash scripting 12 – Files and Grep

Rather than make a video for this, I decided to just make a blog post so that I could include downloadable or at least copy / pasteable components.

Grep stands for GNU Regular Expression Parser, In essence and among other things, it can read (or parse) a file and report on contents, or in the case of this, find a specific string of text.

lorem Ipsum, is standard in the printing industry as it is dummy text used to fill on a page. I have pasted below an example and it just happens to explain further.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.~

If you copy and paste the above and save in a text file called lorem.txt we can do some neat stuff with grep and a few other commands. I am not an expert at this, so this is some of what I picked up while researching this post.

Firstly we can find a specific word (or string) in the text with

cat lorem.txt | grep the

we can also do

grep the lorem.txt

Both will search the file lorem.txt, display the file contents and highlight the word 'the' from the text.

This is great, so what else can we do

In a short file, the number of times a word may appear may be less than 5 or 10. So we could just count manually. As discussed in a previous video, the command wc or word count, does what it says and counts the number of words.

So I found the following

cat lorem.txt | grep -o the | wc -l

Which gives the output as 6 which is how many times the word 'the' appears in the text.

As with other commands, there is a man page so

man grep

and

man wc

Should provide useful information, you can also search for information with duckduckgo and there are numberous tutorials on line.

Hope this is useful

Chat

I am on the Devon and Cornwall Linux user group mailing list and also their matrix channel as zleap, it is better to ask there, that way others can answer too.

Tags

#Bash,#Bashscripting,#Files,#TextSearch,#StringSearch,#Grep,#wc,#WordCount


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Script for $\LaTeX$ Tables

I was asked in irc ( ##chemistry on frenode) about a project to create a table for periodic table data in $\LaTeX$.

This post, is meant to be a rough guide. You are expected to do your own research in to any specific features you need.

As there are now about 120 elements, doing this manually could be rather long winded. So I am trying to develop a small shell script to help with this process.

Firstly Looking at loops in Bash

now how to generate a basic table in $\LaTeX$

\begin{table}[]
\begin{tabular}{llllll}
 &  &  &  &  & 
\end{tabular}
\caption{}
\label{tab:my-table}
\end{table}

This is for a 1 row table, to add multiple rows, you need to add \ at the end of the row as in:-

\begin{table}[]
\begin{tabular}{llllll}
 
 &  &  &  &  &  \\
 &  &  &  &  &  \\
 &  &  &  &  & 
\end{tabular}
\caption{}
\label{tab:my-table}
\end{table}

The following shell script should help us with this.

Please note that the \, should be removed from the very last row.

I have added a script for 10 rows. This can be changed via the shell script.

 1 #!/bin/bash
  2 
  3 echo " " > data.txt
  4 echo "\begin{table}[]" > data.txt
  5 echo "\begin{tabular}{llllll}" >> data.txt
  6 #wc -l data.txt
  7 
  8 for i in {1..10..1}
  9   do 
 10 
 11      #echo "hello"
 12      echo  "&  &  &  &  & \\" >> data.txt
 13  done
 14 #wc -l data.txt
 15  echo  "\end{tabular}" >> data.txt
 16  echo  "\caption{}" >> data.txt
 17  echo  "\label{tab:my-table}" >> data.txt
 18  echo  "\end{table}" >> data.txt

Required table headings were : atom ; r atomic ; density ; Tf ; Tg ;... ?

Table headings are obviously at the top, therefore, once you have run the script and generated the right number of rows amending the first row to

& atom & r atomic & density & Tf & Tg \

Should give the required headings

I have added some word count (wc) to help with diagnostics.

You will need to change the output file extension to .tex for a $\LaTeX$ file. I'll leave it as data.txt as you need to add the other components of a $\LaTeX$ document anyway.

If the table spans more than 1 page then the table should use

\begin{longtable}{llllll}
\end{longtable}

#chemistry, #typesetting, #bash, #LaTeX, #tables, #scripting

You can find me on Friendica at zleap@social.isurf.ca


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay