sâmbătă, mai 05, 2018

How to rename files and move them in subdirectory using bash and git


Files rename

The below script will find all files sufixed with "test.js" and will change "test" sufix to "spec"

#!/bin/bash
find . -name "*.test.js" | while read -r FILE; do
FILENEWNAME=${FILE/.test.js/.spec.js}
echo $FILE
#echo $FILENEWNAME
#replace sufix
git mv "$FILE" "$FILENEWNAME"
done


Files Move

The below script will find files sufixed with "spec.js" and will move them to subdirectory "__test__" of the same location, if file is already moved it is ignored

#!/bin/bash
find . -name "*.spec.js" | while read -r FILE; do 
if [[ "$FILE" != *"__tests__"* ]]; then
echo $FILE
DIRNAME=${FILE%/*}
#create subfolder and move file
mkdir -p "$DIRNAME/__tests__" && git mv "$FILE" "$DIRNAME/__tests__/"
fi
done


duminică, aprilie 22, 2018

How to transform JSON Parent, Child to CSV or TSV

Here is the Javascript code:

```javascript
const json ={
  "objects": [{
      "poolname": "Pool One",
      "ip": "10.10.10.10",
      "groups": [{
          "groupname": "Pool 1 Group 1",
          "ip": "100.100.100.100"
      }, {
          "groupname": "Pool 1 Group 2",
          "ip": "150.150.150.150"   
      }]
    },
    {
      "poolname": "Pool Two",
      "ip": "20.20.20.20",
      "groups": [{
          "groupname": "Pool 2 Group 1",
          "ip": "200.200.200.200"
      }, {
          "groupname": "Pool 2 Group 2",
          "ip": "250.250.250.250"   
      }]
    }]
}

const mapOfMap = (separator)=>[].concat(...json.objects.map(o=>o.groups.map(g=>o.poolname+separator+o.ip+separator+g.groupname+separator+g.ip)))
const toLines = l => l.reduce((acc,v)=>acc+v+"\r\n",'')
const csv = toLines(mapOfMap(','))
const tsv = toLines(mapOfMap('\t'))
console.log(csv)
console.log(tsv)
```

also see the ramda share https://goo.gl/EAM36j where you can run the above code

Membri

Comentarii