Posts from the ‘Unix’ Category

Structured Data in C programming

I was learning about Structured Data and I have found some plenty of weird
code. Here is the simplest code I have found, so everyone can understand 
the concepts.
#include

struct person
{
  char *name;
  int age;
};

int main()
{
  struct person p;
  p.name = "John Smith";
  p.age = 25;
  printf("%s",p.name);
  printf("%d",p.age);
  return 0;
}

Additionally, I have wrote this more elaborated code
(based on the ebook Learning GNU C from Ciarán O'Riordan):

#include 

struct Person
{
  char *name;
  int age;
  int height_in_cm;
};

int
main()
{
  struct Person hero = {"Robin Hood",20, 191 };
  struct Person sidekick;

  sidekick.age = 31;
  sidekick.name = "John Little";
  sidekick.height_in_cm = 237;

  printf("%s is %d years old and stands %dcm tall in his socks\n",
         sidekick.name, sidekick.age, sidekick.height_in_cm);

  printf("He is often seen with %s. \n", hero.name);

  return 0;
}

If you are on Linux, just do it to compile:
#gcc -Wall -o code code.c

Editing date format with sed

So, a few days ago at work I had this problem, I had a file containing many lines of dates in the following format:

mm-dd-yyyy but I had to convert it to yyyy-mm-dd and then join the sed and it’s arguments in a shell script (but I will not cover this shell script today, it is out of focus here).

To solve my problem, I just did the following:

#sed -e “s_\(..\)\-\(..\)\-\(….\)_\3-\1-\2_” sample.txt

Once I did it, I had my date format converted to the new one and sent to the output. So if you want it to be inserted in a new file, just add “>” after the file name, just like this:

#sed -e “s_\(..\)\-\(..\)\-\(….\)_\3-\1-\2_” sample.txt > sample_new.txt

Now you’ve got a new file containing ALL the lines in the new format! 😀

References:

http://www.grymoire.com/Unix/Sed.html